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",
"name": "CyTube",
"description": "Online media synchronizer and chat",
"version": "3.23.3",
"version": "3.23.4",
"repository": {
"url": "http://github.com/calzoneman/sync"
},

View File

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

View File

@ -31,14 +31,6 @@ window.loadMediaPlayer = (data) ->
window.PLAYER = new VideoJSPlayer(data)
catch 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
try
window.PLAYER = TYPE_MAP[data.type](data)

View File

@ -679,10 +679,17 @@
}
GoogleDrivePlayer.prototype.load = function(data) {
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) {
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);
@ -694,8 +701,14 @@
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);

View File

@ -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);
}