Merge pull request #123 from DiegoRBaquero/patch-1

Reconnect timeout improvement
This commit is contained in:
Feross Aboukhadijeh 2016-03-15 21:41:01 -07:00
commit 5d4cf75e0a

View File

@ -15,8 +15,9 @@ var Tracker = require('./tracker')
// boost, and saves browser resources. // boost, and saves browser resources.
var socketPool = {} var socketPool = {}
var RECONNECT_MINIMUM = 15 * 1000
var RECONNECT_MAXIMUM = 30 * 60 * 1000
var RECONNECT_VARIANCE = 30 * 1000 var RECONNECT_VARIANCE = 30 * 1000
var RECONNECT_MINIMUM = 5 * 1000
var OFFER_TIMEOUT = 50 * 1000 var OFFER_TIMEOUT = 50 * 1000
inherits(WebSocketTracker, Tracker) inherits(WebSocketTracker, Tracker)
@ -29,6 +30,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()
} }
@ -134,6 +136,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())
} }
} }
@ -285,10 +288,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 = Math.floor(Math.random() * RECONNECT_VARIANCE) + Math.min(Math.pow(2, self.retries) * RECONNECT_MINIMUM, RECONNECT_MAXIMUM)
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()