Adjust retry mechanism

This commit is contained in:
Calvin Montgomery 2018-06-23 16:54:26 -07:00
parent 07502575eb
commit 2340da1cb5
3 changed files with 31 additions and 13 deletions

View File

@ -1296,7 +1296,7 @@ function checkLetsEncrypt(socketConfig, nonLetsEncryptError) {
} }
function initWS() { function initWS() {
window.socket = new WSShim(new WebSocket('ws://localhost:3000/')); window.socket = new WSShim('ws://localhost:3000/');
setupCallbacks(); setupCallbacks();
} }

View File

@ -3304,13 +3304,13 @@ function maybePromptToUpgradeUserscript() {
function backoffRetry(fn, cb, options) { function backoffRetry(fn, cb, options) {
var jitter = options.jitter || 0; var jitter = options.jitter || 0;
var factor = options.factor || 1; var factor = options.factor || 1;
var currentFactor = factor;
var isRetryable = options.isRetryable || function () { return true; }; var isRetryable = options.isRetryable || function () { return true; };
var maxDelay = options.maxDelay || Infinity; var maxDelay = options.maxDelay || Infinity;
var tries = 0; var tries = 0;
function callback(error, result) { function callback(error, result) {
tries++; tries++;
factor *= factor;
if (error) { if (error) {
if (tries >= options.maxTries) { if (tries >= options.maxTries) {
console.log('Max tries exceeded'); console.log('Max tries exceeded');
@ -3318,7 +3318,7 @@ function backoffRetry(fn, cb, options) {
} else if (isRetryable(error)) { } else if (isRetryable(error)) {
var offset = Math.random() * jitter; var offset = Math.random() * jitter;
var delay = Math.min( var delay = Math.min(
options.delay * factor, options.delay * currentFactor,
maxDelay maxDelay
) + offset; ) + offset;
console.log('Retrying on error: ' + error); console.log('Retrying on error: ' + error);
@ -3331,6 +3331,8 @@ function backoffRetry(fn, cb, options) {
} else { } else {
cb(error, result); cb(error, result);
} }
currentFactor *= factor;
} }
fn(callback); fn(callback);

View File

@ -2,17 +2,15 @@
var TYPE_FRAME = 0; var TYPE_FRAME = 0;
var TYPE_ACK = 1; var TYPE_ACK = 1;
function WSShim(ws) { function WSShim(url) {
this._ws = ws; this._url = url;
this._listeners = Object.create(null); this._listeners = Object.create(null);
this._connected = false; this._connected = false;
this._ws.onopen = this._onopen.bind(this);
this._ws.onclose = this._onclose.bind(this);
this._ws.onmessage = this._onmessage.bind(this);
this._ackId = 0; this._ackId = 0;
this._pendingAcks = Object.create(null); this._pendingAcks = Object.create(null);
this._openWS();
} }
WSShim.prototype.listeners = function listeners(frame) { WSShim.prototype.listeners = function listeners(frame) {
@ -78,17 +76,23 @@
return; return;
} }
this._connected = false;
this._emit('disconnect'); this._emit('disconnect');
// TODO: checking for KICKED here is insufficient;
// need to have some sort of explicit disconnect vs. connection loss
// check
if (!KICKED) { if (!KICKED) {
function reconnectAsync(cb) { var self = this;
initWS();
window.socket._ws.addEventListener('open', function () { function reconnectAsync(cb) {
self._openWS();
self._ws.addEventListener('open', function () {
cb(null); cb(null);
}); });
window.socket._ws.addEventListener('error', function (error) { self._ws.addEventListener('error', function (error) {
cb(error); cb(error);
}); });
} }
@ -128,5 +132,17 @@
} }
}; };
WSShim.prototype._openWS = function _openWS() {
if (this._connected) {
throw new Error('Cannot _openWS() when already connected');
}
this._ws = new WebSocket(this._url);
this._ws.onopen = this._onopen.bind(this);
this._ws.onclose = this._onclose.bind(this);
this._ws.onmessage = this._onmessage.bind(this);
this._connected = false;
};
window.WSShim = WSShim; window.WSShim = WSShim;
})(); })();