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

Clamp Retry-After time to retry.maxRetryAfter #603

Merged
merged 2 commits into from
Jun 26, 2024
Merged
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
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ An object representing `limit`, `methods`, `statusCodes` and `maxRetryAfter` fie

If `retry` is a number, it will be used as `limit` and other defaults will remain in place.

If `maxRetryAfter` is set to `undefined`, it will use `options.timeout`. If [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) header is greater than `maxRetryAfter`, it will cancel the request.
If `maxRetryAfter` is set to `undefined`, it will use `options.timeout`. If [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) header is greater than `maxRetryAfter`, it will use `maxRetryAfter`.

The `backoffLimit` option is the upper limit of the delay per retry in milliseconds.
To clamp the delay, set `backoffLimit` to 1000, for example.
Expand Down
11 changes: 3 additions & 8 deletions source/core/Ky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,18 +219,13 @@ export class Ky {

const retryAfter = error.response.headers.get('Retry-After');
if (retryAfter && this._options.retry.afterStatusCodes.includes(error.response.status)) {
let after = Number(retryAfter);
let after = Number(retryAfter) * 1000;
if (Number.isNaN(after)) {
after = Date.parse(retryAfter) - Date.now();
} else {
after *= 1000;
}

if (this._options.retry.maxRetryAfter !== undefined && after > this._options.retry.maxRetryAfter) {
return 0;
}

return after;
const max = this._options.retry.maxRetryAfter ?? after;
return after < max ? after : max;
}

if (error.response.status === 413) {
Expand Down
72 changes: 44 additions & 28 deletions test/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,44 +221,54 @@ test('respect retry methods', async t => {
});

test('respect maxRetryAfter', async t => {
const retryCount = 4;
let requestCount = 0;

const server = await createHttpTestServer();
server.get('/', async (_request, response) => {
server.get('/', (_request, response) => {
requestCount++;

response.writeHead(413, {
'Retry-After': 1,
});
if (requestCount === retryCount + 1) {
response.end(fixture);
} else {
response.writeHead(413, {
'Retry-After': 1,
});

response.end('');
response.end('');
}
});

await t.throwsAsync(
ky(server.url, {
retry: {
limit: 5,
maxRetryAfter: 100,
},
}).text(),
{
message: /Payload Too Large/,
await withPerformance({
t,
expectedDuration: 420 + 420 + 420 + 420,
async test() {
t.is(await ky(server.url, {
retry: {
limit: retryCount,
maxRetryAfter: 420,
},
}).text(), fixture);
},
);
t.is(requestCount, 1);
});

t.is(requestCount, 5);

requestCount = 0;
await t.throwsAsync(
ky(server.url, {
retry: {
limit: 4,
maxRetryAfter: 2000,
},
}).text(),
{
message: /Payload Too Large/,

await withPerformance({
t,
expectedDuration: 1000 + 1000 + 1000 + 1000,
async test() {
t.is(await ky(server.url, {
retry: {
limit: retryCount,
maxRetryAfter: 2000,
},
}).text(), fixture);
},
);
});

t.is(requestCount, 5);

await server.close();
Expand Down Expand Up @@ -467,6 +477,8 @@ test('respect maximum backoffLimit', async t => {
},
});

t.is(requestCount, 5);

requestCount = 0;

await withPerformance({
Expand All @@ -482,18 +494,20 @@ test('respect maximum backoffLimit', async t => {
},
});

t.is(requestCount, 5);

await server.close();
});

test('respect custom retry.delay', async t => {
const retryCount = 5;
const retryCount = 4;
let requestCount = 0;

const server = await createHttpTestServer();
server.get('/', (_request, response) => {
requestCount++;

if (requestCount === retryCount) {
if (requestCount === retryCount + 1) {
response.end(fixture);
} else {
response.sendStatus(500);
Expand All @@ -513,5 +527,7 @@ test('respect custom retry.delay', async t => {
},
});

t.is(requestCount, 5);

await server.close();
});