Reconnect timeout increase lineally

Minimum set at 15 seconds, each retry adds 30 seconds.
This commit is contained in:
Diego Rodríguez Baquero 2016-03-11 04:12:09 -03:00
parent 18de34050b
commit 8e2dbdc576

View File

@ -15,8 +15,8 @@ var Tracker = require('./tracker')
// boost, and saves browser resources. // boost, and saves browser resources.
var socketPool = {} var socketPool = {}
var RECONNECT_VARIANCE = 30 * 1000 var RECONNECT_MINIMUM = 15 * 1000
var RECONNECT_MINIMUM = 5 * 1000 var RECONNECT_RETRY = 30 * 1000
var OFFER_TIMEOUT = 50 * 1000 var OFFER_TIMEOUT = 50 * 1000
inherits(WebSocketTracker, Tracker) inherits(WebSocketTracker, Tracker)
@ -29,6 +29,7 @@ function WebSocketTracker (client, announceUrl, opts) {
self.peers = {} // peers (offer id -> peer) self.peers = {} // peers (offer id -> peer)
self.socket = null self.socket = null
self.reconnecting = false self.reconnecting = false
self.retries = 0
self._openSocket() self._openSocket()
} }
@ -118,6 +119,7 @@ WebSocketTracker.prototype._onSocketConnect = function () {
if (self.reconnecting) { if (self.reconnecting) {
self.reconnecting = false self.reconnecting = false
self.retries = 0
self.announce(self.client._defaultAnnounceOpts()) self.announce(self.client._defaultAnnounceOpts())
} }
} }
@ -233,10 +235,11 @@ WebSocketTracker.prototype._onSocketError = function (err) {
WebSocketTracker.prototype._startReconnectTimer = function () { WebSocketTracker.prototype._startReconnectTimer = function () {
var self = this var self = this
var ms = Math.floor(Math.random() * RECONNECT_VARIANCE) + RECONNECT_MINIMUM var ms = RECONNECT_MINIMUM + (self.retries * RECONNECT_RETRY)
self.reconnecting = true self.reconnecting = true
var reconnectTimer = setTimeout(function () { var reconnectTimer = setTimeout(function () {
self.retries++
self._openSocket() self._openSocket()
}, ms) }, ms)
if (reconnectTimer.unref) reconnectTimer.unref() if (reconnectTimer.unref) reconnectTimer.unref()