Add jitter and retry logic to google drive userscript lookups
This commit is contained in:
parent
d0d2002a5f
commit
3c11ac6cf5
5 changed files with 84 additions and 47 deletions
|
|
@ -679,23 +679,36 @@
|
|||
}
|
||||
|
||||
GoogleDrivePlayer.prototype.load = function(data) {
|
||||
window.maybePromptToUpgradeUserscript();
|
||||
if (!window.hasDriveUserscript && !data.meta.direct) {
|
||||
window.promptToInstallDriveUserscript();
|
||||
} else if (window.hasDriveUserscript) {
|
||||
window.maybePromptToUpgradeUserscript();
|
||||
}
|
||||
if (typeof window.getGoogleDriveMetadata === 'function') {
|
||||
return window.getGoogleDriveMetadata(data.id, (function(_this) {
|
||||
return function(error, metadata) {
|
||||
var alertBox;
|
||||
if (error) {
|
||||
console.error(error);
|
||||
alertBox = window.document.createElement('div');
|
||||
alertBox.className = 'alert alert-danger';
|
||||
alertBox.textContent = error;
|
||||
return document.getElementById('ytapiplayer').appendChild(alertBox);
|
||||
} else {
|
||||
data.meta.direct = metadata.videoMap;
|
||||
return GoogleDrivePlayer.__super__.load.call(_this, data);
|
||||
}
|
||||
return setTimeout((function(_this) {
|
||||
return function() {
|
||||
return backoffRetry(function(cb) {
|
||||
return window.getGoogleDriveMetadata(data.id, cb);
|
||||
}, function(error, metadata) {
|
||||
var alertBox;
|
||||
if (error) {
|
||||
console.error(error);
|
||||
alertBox = window.document.createElement('div');
|
||||
alertBox.className = 'alert alert-danger';
|
||||
alertBox.textContent = error;
|
||||
return document.getElementById('ytapiplayer').appendChild(alertBox);
|
||||
} else {
|
||||
data.meta.direct = metadata.videoMap;
|
||||
return GoogleDrivePlayer.__super__.load.call(_this, data);
|
||||
}
|
||||
}, {
|
||||
maxTries: 3,
|
||||
delay: 1000,
|
||||
factor: 1.2,
|
||||
jitter: 500
|
||||
});
|
||||
};
|
||||
})(this));
|
||||
})(this), Math.random() * 1000);
|
||||
} else {
|
||||
return GoogleDrivePlayer.__super__.load.call(this, data);
|
||||
}
|
||||
|
|
@ -1530,17 +1543,6 @@
|
|||
e = error1;
|
||||
return console.error(e);
|
||||
}
|
||||
} else if (data.type === 'gd') {
|
||||
try {
|
||||
if (data.meta.html5hack || window.hasDriveUserscript) {
|
||||
return window.PLAYER = new GoogleDrivePlayer(data);
|
||||
} else {
|
||||
return window.PLAYER = new GoogleDriveYouTubePlayer(data);
|
||||
}
|
||||
} catch (error1) {
|
||||
e = error1;
|
||||
return console.error(e);
|
||||
}
|
||||
} else if (data.type in TYPE_MAP) {
|
||||
try {
|
||||
return window.PLAYER = TYPE_MAP[data.type](data);
|
||||
|
|
|
|||
|
|
@ -3256,3 +3256,34 @@ function maybePromptToUpgradeUserscript() {
|
|||
alertBox.insertBefore(closeButton, alertBox.firstChild)
|
||||
document.getElementById('videowrap').appendChild(alertBox);
|
||||
}
|
||||
|
||||
function backoffRetry(fn, cb, options) {
|
||||
var jitter = options.jitter || 0;
|
||||
var factor = options.factor || 1;
|
||||
var isRetryable = options.isRetryable || function () { return true; };
|
||||
var tries = 0;
|
||||
|
||||
function callback(error, result) {
|
||||
tries++;
|
||||
factor *= factor;
|
||||
if (error) {
|
||||
if (tries >= options.maxTries) {
|
||||
console.log('Max tries exceeded');
|
||||
cb(error, result);
|
||||
} else if (isRetryable(error)) {
|
||||
var offset = Math.random() * jitter;
|
||||
var delay = options.delay * factor + offset;
|
||||
console.log('Retrying on error: ' + error);
|
||||
console.log('Waiting ' + delay + ' ms before retrying');
|
||||
|
||||
setTimeout(function () {
|
||||
fn(callback);
|
||||
}, delay);
|
||||
}
|
||||
} else {
|
||||
cb(error, result);
|
||||
}
|
||||
}
|
||||
|
||||
fn(callback);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue