Add jitter and retry logic to google drive userscript lookups

This commit is contained in:
Calvin Montgomery 2016-10-08 10:33:18 -07:00
parent d0d2002a5f
commit 3c11ac6cf5
5 changed files with 84 additions and 47 deletions

View File

@ -2,7 +2,7 @@
"author": "Calvin Montgomery", "author": "Calvin Montgomery",
"name": "CyTube", "name": "CyTube",
"description": "Online media synchronizer and chat", "description": "Online media synchronizer and chat",
"version": "3.23.3", "version": "3.23.4",
"repository": { "repository": {
"url": "http://github.com/calzoneman/sync" "url": "http://github.com/calzoneman/sync"
}, },

View File

@ -6,18 +6,30 @@ window.GoogleDrivePlayer = class GoogleDrivePlayer extends VideoJSPlayer
super(data) super(data)
load: (data) -> load: (data) ->
window.maybePromptToUpgradeUserscript() if not window.hasDriveUserscript and not data.meta.direct
window.promptToInstallDriveUserscript()
else if window.hasDriveUserscript
window.maybePromptToUpgradeUserscript()
if typeof window.getGoogleDriveMetadata is 'function' if typeof window.getGoogleDriveMetadata is 'function'
window.getGoogleDriveMetadata(data.id, (error, metadata) => setTimeout(=>
if error backoffRetry((cb) ->
console.error(error) window.getGoogleDriveMetadata(data.id, cb)
alertBox = window.document.createElement('div') , (error, metadata) =>
alertBox.className = 'alert alert-danger' if error
alertBox.textContent = error console.error(error)
document.getElementById('ytapiplayer').appendChild(alertBox) alertBox = window.document.createElement('div')
else alertBox.className = 'alert alert-danger'
data.meta.direct = metadata.videoMap alertBox.textContent = error
super(data) document.getElementById('ytapiplayer').appendChild(alertBox)
) else
data.meta.direct = metadata.videoMap
super(data)
, {
maxTries: 3
delay: 1000
factor: 1.2
jitter: 500
})
, Math.random() * 1000)
else else
super(data) super(data)

View File

@ -31,14 +31,6 @@ window.loadMediaPlayer = (data) ->
window.PLAYER = new VideoJSPlayer(data) window.PLAYER = new VideoJSPlayer(data)
catch e catch e
console.error e console.error e
else if data.type is 'gd'
try
if data.meta.html5hack or window.hasDriveUserscript
window.PLAYER = new GoogleDrivePlayer(data)
else
window.PLAYER = new GoogleDriveYouTubePlayer(data)
catch e
console.error e
else if data.type of TYPE_MAP else if data.type of TYPE_MAP
try try
window.PLAYER = TYPE_MAP[data.type](data) window.PLAYER = TYPE_MAP[data.type](data)

View File

@ -679,23 +679,36 @@
} }
GoogleDrivePlayer.prototype.load = function(data) { 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') { if (typeof window.getGoogleDriveMetadata === 'function') {
return window.getGoogleDriveMetadata(data.id, (function(_this) { return setTimeout((function(_this) {
return function(error, metadata) { return function() {
var alertBox; return backoffRetry(function(cb) {
if (error) { return window.getGoogleDriveMetadata(data.id, cb);
console.error(error); }, function(error, metadata) {
alertBox = window.document.createElement('div'); var alertBox;
alertBox.className = 'alert alert-danger'; if (error) {
alertBox.textContent = error; console.error(error);
return document.getElementById('ytapiplayer').appendChild(alertBox); alertBox = window.document.createElement('div');
} else { alertBox.className = 'alert alert-danger';
data.meta.direct = metadata.videoMap; alertBox.textContent = error;
return GoogleDrivePlayer.__super__.load.call(_this, data); 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 { } else {
return GoogleDrivePlayer.__super__.load.call(this, data); return GoogleDrivePlayer.__super__.load.call(this, data);
} }
@ -1530,17 +1543,6 @@
e = error1; e = error1;
return console.error(e); 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) { } else if (data.type in TYPE_MAP) {
try { try {
return window.PLAYER = TYPE_MAP[data.type](data); return window.PLAYER = TYPE_MAP[data.type](data);

View File

@ -3256,3 +3256,34 @@ function maybePromptToUpgradeUserscript() {
alertBox.insertBefore(closeButton, alertBox.firstChild) alertBox.insertBefore(closeButton, alertBox.firstChild)
document.getElementById('videowrap').appendChild(alertBox); 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);
}