From 0de32efc295f3635b658e9c1589a51caeec707e5 Mon Sep 17 00:00:00 2001 From: Neil Williams Date: Thu, 27 Feb 2014 11:15:59 -0800 Subject: [PATCH] WebSockets: add some jitter to retries. If the websocket server gets restarted or goes down temporarily, we don't want everyone to be retrying at exactly the same time even though we have exponential backoff. --- r2/r2/public/static/js/websocket.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/r2/r2/public/static/js/websocket.js b/r2/r2/public/static/js/websocket.js index 79428a82f..3c73ec5eb 100644 --- a/r2/r2/public/static/js/websocket.js +++ b/r2/r2/public/static/js/websocket.js @@ -10,6 +10,7 @@ r.WebSocket = function (url) { _.extend(r.WebSocket.prototype, Backbone.Events, { _backoffTime: 2000, _maximumRetries: 9, + _retryJitterAmount: 3000, _connect: function () { r.debug('websocket: connecting') @@ -37,7 +38,9 @@ _.extend(r.WebSocket.prototype, Backbone.Events, { _onClose: function (ev) { if (this._connectionAttempts < this._maximumRetries) { - var delay = this._backoffTime * Math.pow(2, this._connectionAttempts) + var baseDelay = this._backoffTime * Math.pow(2, this._connectionAttempts), + jitter = (Math.random() * this._retryJitterAmount) - (this._retryJitterAmount / 2), + delay = Math.round(baseDelay + jitter) r.debug('websocket: connection lost, reconnecting in ' + delay + 'ms') this.trigger('reconnecting', delay) setTimeout(_.bind(this._connect, this), delay)