From a97d921d84930b2f3c8d9f13d9ae4dbac33cd486 Mon Sep 17 00:00:00 2001 From: Jean Aunis Date: Mon, 20 Jun 2016 14:05:30 +0200 Subject: [PATCH 1/6] Fix simultaneous reconnection handling --- lib/nssocket.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/nssocket.js b/lib/nssocket.js index 762e306..5ca4689 100644 --- a/lib/nssocket.js +++ b/lib/nssocket.js @@ -308,6 +308,9 @@ NsSocket.prototype.connect = function connect(/*port, host, callback*/) { // NsSocket.prototype.reconnect = function reconnect() { var self = this; + if(self.pendingReconnect) + return; + self.pendingReconnect = true; // // Helper function containing the core reconnect logic @@ -341,6 +344,7 @@ NsSocket.prototype.reconnect = function reconnect() { // it is less than the maximum // function tryReconnect() { + self.pendingReconnect = false; self.retry.retries++; if (self.retry.retries >= self.retry.max) { return self.emit('error', new Error('Did not reconnect after maximum retries: ' + self.retry.max)); From d31217808c2726ccec344144f153a9c3e18810ab Mon Sep 17 00:00:00 2001 From: Jean Aunis Date: Mon, 12 Sep 2016 17:43:14 +0200 Subject: [PATCH 2/6] Limit reconnection interval to five times the configured interval, instead of growing exponentially --- lib/nssocket.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nssocket.js b/lib/nssocket.js index 5ca4689..297e3c9 100644 --- a/lib/nssocket.js +++ b/lib/nssocket.js @@ -353,7 +353,7 @@ NsSocket.prototype.reconnect = function reconnect() { doReconnect(); } - this.retry.wait = this.retry.interval * Math.pow(10, this.retry.retries); + this.retry.wait = this.retry.interval * (5 - 4*Math.pow(1.5, -this.retry.retries)); setTimeout(tryReconnect, this.retry.wait); }; From 35634b215afecfc897ead889186b448ffc0dd4cd Mon Sep 17 00:00:00 2001 From: Jean Aunis Date: Tue, 11 Oct 2016 16:06:14 +0200 Subject: [PATCH 3/6] =?UTF-8?q?-=20Add=20comment=20about=20Sigmo=C3=AFd=20?= =?UTF-8?q?function=20-=20Move=20attribute=20pendingReconnect=20to=20the?= =?UTF-8?q?=20retry=20object?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/nssocket.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lib/nssocket.js b/lib/nssocket.js index 297e3c9..901415e 100644 --- a/lib/nssocket.js +++ b/lib/nssocket.js @@ -61,7 +61,8 @@ var NsSocket = exports.NsSocket = function (socket, options) { retries: 0, max: options.maxRetries || 10, interval: options.retryInterval || 5000, - wait: options.retryInterval || 5000 + wait: options.retryInterval || 5000, + pendingReconnect: false }; // @@ -308,9 +309,9 @@ NsSocket.prototype.connect = function connect(/*port, host, callback*/) { // NsSocket.prototype.reconnect = function reconnect() { var self = this; - if(self.pendingReconnect) + if(self.retry.pendingReconnect) return; - self.pendingReconnect = true; + self.retry.pendingReconnect = true; // // Helper function containing the core reconnect logic @@ -344,7 +345,7 @@ NsSocket.prototype.reconnect = function reconnect() { // it is less than the maximum // function tryReconnect() { - self.pendingReconnect = false; + self.retry.pendingReconnect = false; self.retry.retries++; if (self.retry.retries >= self.retry.max) { return self.emit('error', new Error('Did not reconnect after maximum retries: ' + self.retry.max)); @@ -353,6 +354,8 @@ NsSocket.prototype.reconnect = function reconnect() { doReconnect(); } + // retry interval follows a kind of Sigmoid function + // it will be limited to 5 * (initial interval) this.retry.wait = this.retry.interval * (5 - 4*Math.pow(1.5, -this.retry.retries)); setTimeout(tryReconnect, this.retry.wait); }; From fde3816b6c24ffc9df71bd57658593a1d8da5f41 Mon Sep 17 00:00:00 2001 From: Jean Aunis Date: Fri, 4 Nov 2016 09:35:29 +0100 Subject: [PATCH 4/6] Reconnect indefinitely when a negative maxRetries is provided --- lib/nssocket.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/nssocket.js b/lib/nssocket.js index 901415e..00d1700 100644 --- a/lib/nssocket.js +++ b/lib/nssocket.js @@ -347,7 +347,7 @@ NsSocket.prototype.reconnect = function reconnect() { function tryReconnect() { self.retry.pendingReconnect = false; self.retry.retries++; - if (self.retry.retries >= self.retry.max) { + if (self.retry.max >= 0 && self.retry.retries >= self.retry.max) { return self.emit('error', new Error('Did not reconnect after maximum retries: ' + self.retry.max)); } From e2eaa8abe3351621fe2a266c7373d8bd5cd45383 Mon Sep 17 00:00:00 2001 From: Jean Aunis Date: Thu, 8 Feb 2018 17:31:43 +0100 Subject: [PATCH 5/6] Re-emit start event when reconnecting --- lib/nssocket.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/nssocket.js b/lib/nssocket.js index 00d1700..b0ea244 100644 --- a/lib/nssocket.js +++ b/lib/nssocket.js @@ -331,6 +331,7 @@ NsSocket.prototype.reconnect = function reconnect() { self.socket.once('connect', function () { self.retry.waiting = false; self.retry.retries = 0; + self._onStart(); }); // From dd2c4af67ae84f5a397d5a259baafc41fc1512d1 Mon Sep 17 00:00:00 2001 From: Jean Aunis Date: Wed, 14 Mar 2018 12:43:43 +0100 Subject: [PATCH 6/6] Fix reconnection handling : - timeout is given in seconds - additional security to avoid launching several reconnections simultaneously --- lib/nssocket.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/nssocket.js b/lib/nssocket.js index b0ea244..5f72b13 100644 --- a/lib/nssocket.js +++ b/lib/nssocket.js @@ -321,15 +321,14 @@ NsSocket.prototype.reconnect = function reconnect() { // Cleanup and recreate the socket associated // with this instance. // - self.retry.waiting = true; self.socket.removeAllListeners(); self.socket = common.createSocket(self._options); + self.retry.pendingReconnect = false; // // Cleanup reconnect logic once the socket connects // self.socket.once('connect', function () { - self.retry.waiting = false; self.retry.retries = 0; self._onStart(); }); @@ -346,7 +345,6 @@ NsSocket.prototype.reconnect = function reconnect() { // it is less than the maximum // function tryReconnect() { - self.retry.pendingReconnect = false; self.retry.retries++; if (self.retry.max >= 0 && self.retry.retries >= self.retry.max) { return self.emit('error', new Error('Did not reconnect after maximum retries: ' + self.retry.max)); @@ -358,7 +356,9 @@ NsSocket.prototype.reconnect = function reconnect() { // retry interval follows a kind of Sigmoid function // it will be limited to 5 * (initial interval) this.retry.wait = this.retry.interval * (5 - 4*Math.pow(1.5, -this.retry.retries)); - setTimeout(tryReconnect, this.retry.wait); + if(this.retry.reconnectTimeout) + clearTimeout(this.retry.reconnectTimeout); + this.retry.reconnectTimeout = setTimeout(tryReconnect, this.retry.wait * 1000); }; //