Skip to content
Myr

Add `retry.delay` option to control the time between retries (#533)

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

authored by Russell Clarey · committed by GitHub · parent 577dd4c · Browse files · 7 files+106 added−35 removed
This commit

State

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

Diff stats

7 files+106 added−35 removed

  • tests:+92 added−32 removed
  • other:+14 added−3 removed

7 files+106 added−35 removed

modified:source/core/Ky.ts+2 added-2 removed
232 unmodified lines
233 return 0;234 }235 }236237 const BACKOFF_FACTOR = 0.3;238 return Math.min(this._options.retry.backoffLimit, BACKOFF_FACTOR * (2 ** (this._retryCount - 1)) * 1000);237 const retryDelay = this._options.retry.delay(this._retryCount);238 return Math.min(this._options.retry.backoffLimit, retryDelay);239 }240241 return 0;242 }
118 unmodified lines
modified:source/types/options.ts+1 added-1 removed
136 unmodified lines
137 If `retry` is a number, it will be used as `limit` and other defaults will remain in place.138139 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.140141 Delays between retries is calculated with the function `0.3 * (2 ** (retry - 1)) * 1000`, where `retry` is the attempt number (starts from 1).141 By default, delays between retries are calculated with the function `0.3 * (2 ** (attemptCount - 1)) * 1000`, where `attemptCount` is the attempt number (starts from 1), however this can be changed by passing a `delay` function.142143 Retries are not triggered following a timeout.144145 @example
105 unmodified lines
modified:source/types/retry.ts+7 added
modified:source/utils/normalize.ts+1 added
added:test/helpers/with-performance-observer.ts+42 added
modified:test/retry.ts+50 added-32 removed
1import {performance, PerformanceObserver} from 'node:perf_hooks';2import process from 'node:process';1import test from 'ava';2import ky from '../source/index.js';3import {createHttpTestServer} from './helpers/create-http-test-server.js';4import {withPerformanceObserver} from './helpers/with-performance-observer.js';56const fixture = 'fixture';7const defaultRetryCount = 2;8const retryAfterOn413 = 2;
447 unmodified lines
456 response.sendStatus(500);457 }458 });459461 // We allow the test to take more time on CI than locally, to reduce flakiness462 const allowedOffset = process.env.CI ? 1000 : 300;460 await withPerformanceObserver({461 t,462 name: 'default',463 expectedDuration: 300 + 600 + 1200 + 2400,464 async test() {465 t.is(await ky(server.url, {466 retry: retryCount,467 }).text(), fixture);468 },469 });470464 // Register observer that asserts on duration when a measurement is performed465 const obs = new PerformanceObserver(items => {466 const measurements = items.getEntries();471 requestCount = 0;472 await withPerformanceObserver({473 t,474 name: 'custom',475 expectedDuration: 300 + 600 + 1000 + 1000,476 async test() {477 t.is(await ky(server.url, {478 retry: {479 limit: retryCount,480 backoffLimit: 1000,481 },482 }).text(), fixture);483 },484 });485468 const duration = measurements[0].duration ?? Number.NaN;469 const expectedDuration = {default: 300 + 600 + 1200 + 2400, custom: 300 + 600 + 1000 + 1000}[measurements[0].name] ?? Number.NaN;486 await server.close();487});488471 t.true(Math.abs(duration - expectedDuration) < allowedOffset, `Duration of ${duration}ms is not close to expected duration ${expectedDuration}ms`); // Allow for 300ms difference489test('respect custom retry.delay', async t => {490 const retryCount = 5;491 let requestCount = 0;492473 if (measurements[0].name === 'custom') {474 obs.disconnect();493 const server = await createHttpTestServer();494 server.get('/', (_request, response) => {495 requestCount++;496497 if (requestCount === retryCount) {498 response.end(fixture);499 } else {500 response.sendStatus(500);501 }502 });477 obs.observe({entryTypes: ['measure']});503479 // Start measuring480 performance.mark('start');481 t.is(await ky(server.url, {482 retry: retryCount,483 }).text(), fixture);484 performance.mark('end');485486 performance.mark('start-custom');487 requestCount = 0;488 t.is(await ky(server.url, {489 retry: {490 limit: retryCount,491 backoffLimit: 1000,504 await withPerformanceObserver({505 t,506 name: 'linear',507 expectedDuration: 200 + 300 + 400 + 500,508 async test() {509 t.is(await ky(server.url, {510 retry: {511 limit: retryCount,512 delay: n => 100 * (n + 1),513 },514 }).text(), fixture);515 },493 }).text(), fixture);516 });517495 performance.mark('end-custom');496497 performance.measure('default', 'start', 'end');498 performance.measure('custom', 'start-custom', 'end-custom');499518 await server.close();519});
modified:readme.md+3 added
194 unmodified lines
195- `methods`: `get` `put` `head` `delete` `options` `trace`196- `statusCodes`: [`408`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) [`413`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/413) [`429`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) [`500`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/500) [`502`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/502) [`503`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/503) [`504`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/504)197- `maxRetryAfter`: `undefined`198- `backoffLimit`: `undefined`199- `delay`: `attemptCount => 0.3 * (2 ** (attemptCount - 1)) * 1000`200201An 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.202203If `retry` is a number, it will be used as `limit` and other defaults will remain in place.
3 unmodified lines
207The `backoffLimit` option is the upper limit of the delay per retry in milliseconds.208To clamp the delay, set `backoffLimit` to 1000, for example.209By default, the delay is calculated with `0.3 * (2 ** (attemptCount - 1)) * 1000`. The delay increases exponentially.210211The `delay` option can be used to change how the delay between retries is calculated. The function receives one parameter, the attempt count, starting at `1`.212213Retries are not triggered following a [timeout](#timeout).214215```js216import ky from 'ky';
468 unmodified lines