From 8e2dbdc576d56d2fd05ba072db78cf20710cd9bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Rodr=C3=ADguez=20Baquero?= Date: Fri, 11 Mar 2016 04:12:09 -0300 Subject: [PATCH 1/3] Reconnect timeout increase lineally Minimum set at 15 seconds, each retry adds 30 seconds. --- lib/client/websocket-tracker.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/client/websocket-tracker.js b/lib/client/websocket-tracker.js index 773aa83..cd51aa5 100644 --- a/lib/client/websocket-tracker.js +++ b/lib/client/websocket-tracker.js @@ -15,8 +15,8 @@ var Tracker = require('./tracker') // boost, and saves browser resources. var socketPool = {} -var RECONNECT_VARIANCE = 30 * 1000 -var RECONNECT_MINIMUM = 5 * 1000 +var RECONNECT_MINIMUM = 15 * 1000 +var RECONNECT_RETRY = 30 * 1000 var OFFER_TIMEOUT = 50 * 1000 inherits(WebSocketTracker, Tracker) @@ -29,6 +29,7 @@ function WebSocketTracker (client, announceUrl, opts) { self.peers = {} // peers (offer id -> peer) self.socket = null self.reconnecting = false + self.retries = 0 self._openSocket() } @@ -118,6 +119,7 @@ WebSocketTracker.prototype._onSocketConnect = function () { if (self.reconnecting) { self.reconnecting = false + self.retries = 0 self.announce(self.client._defaultAnnounceOpts()) } } @@ -233,10 +235,11 @@ WebSocketTracker.prototype._onSocketError = function (err) { WebSocketTracker.prototype._startReconnectTimer = function () { var self = this - var ms = Math.floor(Math.random() * RECONNECT_VARIANCE) + RECONNECT_MINIMUM + var ms = RECONNECT_MINIMUM + (self.retries * RECONNECT_RETRY) self.reconnecting = true var reconnectTimer = setTimeout(function () { + self.retries++ self._openSocket() }, ms) if (reconnectTimer.unref) reconnectTimer.unref() From 529203ea40ee5c3e1a788760001e0132a7794377 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Rodr=C3=ADguez=20Baquero?= Date: Fri, 11 Mar 2016 15:26:55 -0300 Subject: [PATCH 2/3] Max reconnect time --- lib/client/websocket-tracker.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/client/websocket-tracker.js b/lib/client/websocket-tracker.js index cd51aa5..e24c9a7 100644 --- a/lib/client/websocket-tracker.js +++ b/lib/client/websocket-tracker.js @@ -16,6 +16,7 @@ var Tracker = require('./tracker') var socketPool = {} var RECONNECT_MINIMUM = 15 * 1000 +var RECONNECT_MAXIMUM = 5 * 60 * 1000 var RECONNECT_RETRY = 30 * 1000 var OFFER_TIMEOUT = 50 * 1000 @@ -235,7 +236,7 @@ WebSocketTracker.prototype._onSocketError = function (err) { WebSocketTracker.prototype._startReconnectTimer = function () { var self = this - var ms = RECONNECT_MINIMUM + (self.retries * RECONNECT_RETRY) + var ms = Math.min(RECONNECT_MINIMUM + (self.retries * RECONNECT_RETRY), RECONNECT_MAXIMUM) self.reconnecting = true var reconnectTimer = setTimeout(function () { From 050d95ac72596b41ace80a474161e9ec49cb3736 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Diego=20Rodr=C3=ADguez=20Baquero?= Date: Wed, 16 Mar 2016 01:14:24 -0300 Subject: [PATCH 3/3] Variance and higher max --- lib/client/websocket-tracker.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/client/websocket-tracker.js b/lib/client/websocket-tracker.js index e24c9a7..de0f101 100644 --- a/lib/client/websocket-tracker.js +++ b/lib/client/websocket-tracker.js @@ -16,8 +16,8 @@ var Tracker = require('./tracker') var socketPool = {} var RECONNECT_MINIMUM = 15 * 1000 -var RECONNECT_MAXIMUM = 5 * 60 * 1000 -var RECONNECT_RETRY = 30 * 1000 +var RECONNECT_MAXIMUM = 30 * 60 * 1000 +var RECONNECT_VARIANCE = 30 * 1000 var OFFER_TIMEOUT = 50 * 1000 inherits(WebSocketTracker, Tracker) @@ -236,7 +236,7 @@ WebSocketTracker.prototype._onSocketError = function (err) { WebSocketTracker.prototype._startReconnectTimer = function () { var self = this - var ms = Math.min(RECONNECT_MINIMUM + (self.retries * RECONNECT_RETRY), RECONNECT_MAXIMUM) + var ms = Math.floor(Math.random() * RECONNECT_VARIANCE) + Math.min(Math.pow(2, self.retries) * RECONNECT_MINIMUM, RECONNECT_MAXIMUM) self.reconnecting = true var reconnectTimer = setTimeout(function () {