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
Provenance
- Commit
f0f9111- Tree
9a14021- Author
Seth Holladay <me@seth-holladay.com>- Committer
GitHub <noreply@github.com>- Parent
c5be841
3 files changed+48 added−37 removed
modified:source/core/Ky.ts+3 added-8 removed
217 unmodified lines218 }219 220 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 }226 229- if (this._options.retry.maxRetryAfter !== undefined && after > this._options.retry.maxRetryAfter) {230- return 0;231- }232-233- return after;227+ const max = this._options.retry.maxRetryAfter ?? after;228+ return after < max ? after : max;229 }230 231 if (error.response.status === 413) {232 return 0;133 unmodified linesmodified:test/retry.ts+44 added-28 removed
219 unmodified lines220 await server.close();221 });222 223 test('respect maxRetryAfter', async t => {224+ const retryCount = 4;225 let requestCount = 0;226 227 const server = await createHttpTestServer();227- server.get('/', async (_request, response) => {228+ server.get('/', (_request, response) => {229 requestCount++;230 231+ 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+ });237 234- response.end('');238+ response.end('');239+ }240 });241 237- 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+ });254 255+ t.is(requestCount, 5);256+257 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/,258+259+ 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+ });271+272 t.is(requestCount, 5);273 274 await server.close();275 });200 unmodified lines476 }).text(), fixture);477 },478 });479 480+ t.is(requestCount, 5);481+482 requestCount = 0;483 484 await withPerformance({485 t,7 unmodified lines493 }).text(), fixture);494 },495 });496 497+ t.is(requestCount, 5);498+499 await server.close();500 });501 502 test('respect custom retry.delay', async t => {489- const retryCount = 5;503+ const retryCount = 4;504 let requestCount = 0;505 506 const server = await createHttpTestServer();507 server.get('/', (_request, response) => {508 requestCount++;509 496- if (requestCount === retryCount) {510+ if (requestCount === retryCount + 1) {511 response.end(fixture);512 } else {513 response.sendStatus(500);514 }11 unmodified lines526 }).text(), fixture);527 },528 });529 530+ t.is(requestCount, 5);531+532 await server.close();533 });modified:readme.md+1 added-1 removed
218 unmodified lines219 An 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.220 221 If `retry` is a number, it will be used as `limit` and other defaults will remain in place.222 223-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.223+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`.224 225 The `backoffLimit` option is the upper limit of the delay per retry in milliseconds.226 To clamp the delay, set `backoffLimit` to 1000, for example.227 By default, the delay is calculated with `0.3 * (2 ** (attemptCount - 1)) * 1000`. The delay increases exponentially.500 unmodified lines