Skip to content

Commit

Permalink
fix: retry endpoint on status code 500
Browse files Browse the repository at this point in the history
previously, it would give up on that endpoint and move on to the next
one. most of the time, a status code 500, a server side error, is a
one-off.
  • Loading branch information
fent committed Nov 16, 2020
1 parent 605c964 commit e1e1af1
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lib/info.js
Expand Up @@ -184,7 +184,7 @@ const retryFunc = async(func, args, options) => {
result = await func(...args);
break;
} catch (err) {
if (err instanceof miniget.MinigetError || currentTry >= options.maxRetries) {
if ((err instanceof miniget.MinigetError && err.statusCode < 500) || currentTry >= options.maxRetries) {
throw err;
}
let wait = Math.min(++currentTry * options.backoff.inc, options.backoff.max);
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -37,7 +37,7 @@
"dependencies": {
"html-entities": "^1.3.1",
"m3u8stream": "^0.8.1",
"miniget": "^3.0.0",
"miniget": "^3.1.0",
"sax": "^1.1.3"
},
"devDependencies": {
Expand Down
7 changes: 2 additions & 5 deletions test/files/refresh.js
Expand Up @@ -64,16 +64,13 @@ const videos = [
{
id: '_HSylqgVYQI',
type: 'regular',
keep: ['video.flv', 'watch-reload-now-2.json'],
keep: ['video.flv', 'watch-reload-now.json', 'watch-reload-now-2.json'],
saveInfo: true,
transform: [
{
page: 'watch.json',
saveAs: 'no-extras',
fn: body => {
body = body.replace('playerMicroformatRenderer', '');
return body;
},
fn: body => body.replace('playerMicroformatRenderer', ''),
},
{
page: 'watch.html',
Expand Down
1 change: 1 addition & 0 deletions test/files/videos/regular/watch-reload-now.json
@@ -0,0 +1 @@
{"reload":"now"}
79 changes: 64 additions & 15 deletions test/info-test.js
Expand Up @@ -345,7 +345,6 @@ describe('ytdl.getInfo()', () => {
const id = 'LuZu9N53Vd0';
const scope = nock(id, 'age-restricted', {
watchJson: [true, 200, 'bad-config'],
watchHtml: false,
});
let info = await ytdl.getInfo(id);
scope.done();
Expand All @@ -371,29 +370,79 @@ describe('ytdl.getInfo()', () => {
});

describe('When watch page gives back `{"reload":"now"}`', () => {
it('Uses backup embed.html page', async() => {
const id = 'LuZu9N53Vd0';
const scope = nock(id, 'age-restricted', {
it('Retries the request', async() => {
const id = '_HSylqgVYQI';
const scope1 = nock(id, 'regular', {
watchJson: [true, 200, 'reload-now'],
});
const scope2 = nock(id, 'age-restricted', {
watchJson: [true, 200, 'reload-now'],
embed: false,
get_video_info: false,
const scope2 = nock(id, 'regular', {
watchHtml: false,
player: false,
});
let info = await ytdl.getInfo(id, {
requestOptions: {
maxRetries: 1,
backoff: { inc: 0 },
},
let info = await ytdl.getInfo(id, { requestOptions: { maxRetries: 1 } });
scope1.done();
scope2.done();
assert.ok(info.formats.length);
assert.ok(info.formats[0].url);
});

describe('Too many times', () => {
it('Uses backup embed.html page', async() => {
const id = 'LuZu9N53Vd0';
const scope = nock(id, 'age-restricted', {
watchJson: [true, 200, 'reload-now'],
});
const scope2 = nock(id, 'age-restricted', {
watchJson: [true, 200, 'reload-now'],
embed: false,
get_video_info: false,
player: false,
});
let info = await ytdl.getInfo(id, {
requestOptions: {
maxRetries: 1,
backoff: { inc: 0 },
},
});
scope.done();
scope2.done();
assert.ok(info.html5player);
assert.ok(info.formats.length);
assert.ok(info.formats[0].url);
});
scope.done();
});
});

describe('When an endpoint gives back a 500 server error', () => {
it('Retries the request', async() => {
const id = '_HSylqgVYQI';
const scope1 = nock(id, 'regular', {
watchJson: [true, 502],
});
const scope2 = nock(id, 'regular', {
watchHtml: false,
player: false,
});
let info = await ytdl.getInfo(id, { requestOptions: { maxRetries: 1 } });
scope1.done();
scope2.done();
assert.ok(info.html5player);
assert.ok(info.formats.length);
assert.ok(info.formats[0].url);
});

describe('Too many times', () => {
it('Uses the next endpoint as backup', async() => {
const id = 'LuZu9N53Vd0';
const scope = nock(id, 'age-restricted', {
watchJson: [true, 502],
});
let info = await ytdl.getInfo(id);
scope.done();
assert.ok(info.html5player);
assert.ok(info.formats.length);
assert.ok(info.formats[0].url);
});
});
});
});

Expand Down

0 comments on commit e1e1af1

Please sign in to comment.