Add max reconnect backoff

Sockets will try to reconnect upon disconnection with an exponentially
rising backoff (configurable). However, if it's unbounded, at a certain
point does it need to even try? Probably not. Instead there's now a
`max` option that defaults to a minute, and the backoff will never
exceed that time.
This commit is contained in:
Jesse Gibson 2016-11-18 12:49:58 -07:00
parent 2696b4cf88
commit 6a63b46d2b

View File

@ -10,6 +10,7 @@ var util = require('util');
* @param {Object} [options] - Override the default settings.
* @param {Object} options.time=50 - Initial backoff time.
* @param {Object} options.factor=2 - How much to multiply the time by.
* @param {Object} options.max=1min - Maximum backoff time.
* @class
*/
function Backoff (options) {
@ -24,7 +25,14 @@ function Backoff (options) {
* @return {Number} - The next backoff time.
*/
Backoff.prototype.next = function () {
this.time *= this.factor;
var next = this.time * this.factor;
if (next > this.max) {
this.time = this.max;
return this.max;
}
this.time = next;
return this.time;
};
@ -38,6 +46,7 @@ Backoff.prototype.reset = function () {
this.time = options.time || 50;
this.factor = options.factor || 2;
this.max = options.max || 1 * 60 * 1000;
return this;
};