Skip to content
Myr

Clamp Retry-After time to retry.maxRetryAfter (#603)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Clamp Retry-After time to retry.maxRetryAfter We now use `maxRetryAfter` as a limit, rather than a threshold for canceling the retry. If `Retry-After` is greater than `maxRetryAfter`, then `Retry-After` will be capped. * Document `maxRetryAfter` changes

authored by Seth Holladay · committed by GitHub · parent c5be841 · Browse files · 3 files+48 added−37 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+48 added−37 removed

  • tests:+44 added−28 removed
  • other:+4 added−9 removed

3 files+48 added−37 removed

modified:source/core/Ky.ts+3 added-8 removed
217 unmodified lines
218 }219220 const retryAfter = error.response.headers.get('Retry-After');221 if (retryAfter && this._options.retry.afterStatusCodes.includes(error.response.status)) {222 let after = Number(retryAfter);222 let after = Number(retryAfter) * 1000;223 if (Number.isNaN(after)) {224 after = Date.parse(retryAfter) - Date.now();225 } else {226 after *= 1000;225 }226229 if (this._options.retry.maxRetryAfter !== undefined && after > this._options.retry.maxRetryAfter) {230 return 0;231 }232233 return after;227 const max = this._options.retry.maxRetryAfter ?? after;228 return after < max ? after : max;229 }230231 if (error.response.status === 413) {232 return 0;
133 unmodified lines
modified:test/retry.ts+44 added-28 removed
219 unmodified lines
220 await server.close();221});222223test('respect maxRetryAfter', async t => {224 const retryCount = 4;225 let requestCount = 0;226227 const server = await createHttpTestServer();227 server.get('/', async (_request, response) => {228 server.get('/', (_request, response) => {229 requestCount++;230231 if (requestCount === retryCount + 1) {232 response.end(fixture);233 } else {230 response.writeHead(413, {231 'Retry-After': 1,232 });234 response.writeHead(413, {235 'Retry-After': 1,236 });237234 response.end('');238 response.end('');239 }240 });241237 await t.throwsAsync(238 ky(server.url, {239 retry: {240 limit: 5,241 maxRetryAfter: 100,242 },243 }).text(),244 {245 message: /Payload Too Large/,242 await withPerformance({243 t,244 expectedDuration: 420 + 420 + 420 + 420,245 async test() {246 t.is(await ky(server.url, {247 retry: {248 limit: retryCount,249 maxRetryAfter: 420,250 },251 }).text(), fixture);252 },247 );248 t.is(requestCount, 1);253 });254255 t.is(requestCount, 5);256257 requestCount = 0;251 await t.throwsAsync(252 ky(server.url, {253 retry: {254 limit: 4,255 maxRetryAfter: 2000,256 },257 }).text(),258 {259 message: /Payload Too Large/,258259 await withPerformance({260 t,261 expectedDuration: 1000 + 1000 + 1000 + 1000,262 async test() {263 t.is(await ky(server.url, {264 retry: {265 limit: retryCount,266 maxRetryAfter: 2000,267 },268 }).text(), fixture);269 },261 );270 });271272 t.is(requestCount, 5);273274 await server.close();275});
200 unmodified lines
476 }).text(), fixture);477 },478 });479480 t.is(requestCount, 5);481482 requestCount = 0;483484 await withPerformance({485 t,
7 unmodified lines
493 }).text(), fixture);494 },495 });496497 t.is(requestCount, 5);498499 await server.close();500});501502test('respect custom retry.delay', async t => {489 const retryCount = 5;503 const retryCount = 4;504 let requestCount = 0;505506 const server = await createHttpTestServer();507 server.get('/', (_request, response) => {508 requestCount++;509496 if (requestCount === retryCount) {510 if (requestCount === retryCount + 1) {511 response.end(fixture);512 } else {513 response.sendStatus(500);514 }
11 unmodified lines
526 }).text(), fixture);527 },528 });529530 t.is(requestCount, 5);531532 await server.close();533});
modified:readme.md+1 added-1 removed
218 unmodified lines
219An object representing `limit`, `methods`, `statusCodes` and `maxRetryAfter` fields for maximum retry count, allowed methods, allowed status codes and maximum [`Retry-After`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After) time.220221If `retry` is a number, it will be used as `limit` and other defaults will remain in place.222223If `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.223If `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`.224225The `backoffLimit` option is the upper limit of the delay per retry in milliseconds.226To clamp the delay, set `backoffLimit` to 1000, for example.227By default, the delay is calculated with `0.3 * (2 ** (attemptCount - 1)) * 1000`. The delay increases exponentially.
500 unmodified lines