Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry-After header, support non-standard formats #556

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,22 @@ export class LinkChecker extends EventEmitter {
}
}
}
// The `retry-after` header can come in either <seconds> or
// A specific date to go check.
private parseRetryAfter(retryAfterRaw: string): number {
let retryAfter = Number(retryAfterRaw) * 1000 + Date.now();
if (!isNaN(retryAfter)) return retryAfter;

retryAfter = Date.parse(retryAfterRaw);
if (!isNaN(retryAfter)) return retryAfter;

// handle invalid response in formats `1m30s` or `30s`
const matches = retryAfterRaw.match(/^(?:(\d+)m)?(\d+)s$/);
if (!matches) return NaN;
return (
(Number(matches[1] || 0) * 60 + Number(matches[2])) * 1000 + Date.now()
);
}
/**
* Check the incoming response for a `retry-after` header. If present,
* and if the status was an HTTP 429, calculate the date at which this
Expand All @@ -404,15 +420,8 @@ export class LinkChecker extends EventEmitter {
return false;
}

// The `retry-after` header can come in either <seconds> or
// A specific date to go check.
let retryAfter = Number(retryAfterRaw) * 1000 + Date.now();
if (isNaN(retryAfter)) {
retryAfter = Date.parse(retryAfterRaw);
if (isNaN(retryAfter)) {
return false;
}
}
const retryAfter = this.parseRetryAfter(retryAfterRaw);
if (isNaN(retryAfter)) return false;

// check to see if there is already a request to wait for this host
if (opts.delayCache.has(opts.url.host)) {
Expand Down
50 changes: 50 additions & 0 deletions test/test.retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,56 @@ describe('retries', () => {
scope.done();
});

it('should retry 429s with invalid `s` suffix', async () => {
const scope = nock('http://fake.local')
.get('/')
.reply(429, undefined, {
'retry-after': '3s',
})
.get('/')
.reply(200);

const {promise, resolve} = invertedPromise();
const checker = new LinkChecker().on('retry', resolve);
const clock = sinon.useFakeTimers({
shouldAdvanceTime: true,
});
const checkPromise = checker.check({
path: 'test/fixtures/basic',
retry: true,
});
await promise;
await clock.tickAsync(3000);
const results = await checkPromise;
assert.ok(results.passed);
scope.done();
});

it('should retry 429s with invalid `1m1s` format', async () => {
const scope = nock('http://fake.local')
.get('/')
.reply(429, undefined, {
'retry-after': '1m1s',
})
.get('/')
.reply(200);

const {promise, resolve} = invertedPromise();
const checker = new LinkChecker().on('retry', resolve);
const clock = sinon.useFakeTimers({
shouldAdvanceTime: true,
});
const checkPromise = checker.check({
path: 'test/fixtures/basic',
retry: true,
});
await promise;
await clock.tickAsync(61000);
const results = await checkPromise;
assert.ok(results.passed);
scope.done();
});

it('should detect requests to wait on the same host', async () => {
const scope = nock('http://fake.local')
.get('/1')
Expand Down