Add `retry.backoffLimit` option (#454)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Frank van Wijk · committed by GitHub · parent bc7d9f4 · Browse files · 5 files·+83 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
Diff stats
5 files+83 added−3 removed
- tests:+59 added−0 removed
- other:+24 added−3 removed
Provenance
- Commit
ab19baf- Tree
3a7fa1e- Author
Frank van Wijk <github@frankvanwijk.nl>- Committer
GitHub <noreply@github.com>- Parent
bc7d9f4
5 files changed+83 added−3 removed
modified:source/core/Ky.ts+1 added-1 removed
233 unmodified lines234 }235 }236 237 const BACKOFF_FACTOR = 0.3;238- return BACKOFF_FACTOR * (2 ** (this._retryCount - 1)) * 1000;238+ return Math.min(this._options.retry.backoffLimit, BACKOFF_FACTOR * (2 ** (this._retryCount - 1)) * 1000);239 }240 241 return 0;242 }119 unmodified linesmodified:source/types/retry.ts+16 added
31 unmodified lines32333435363738394041424344454647484950515231 unmodified lines
@default Infinity */ maxRetryAfter?: number;
/** The upper limit of the delay per retry in milliseconds. To clamp the delay, set `backoffLimit` to 1000, for example.
By default, the delay is calculated in the following way:
``` 0.3 * (2 ** (attemptCount - 1)) * 1000 ```
The delay increases exponentially.
@default Infinity */ backoffLimit?: number;}modified:source/utils/normalize.ts+1 added
15 unmodified lines16171819202122232420 unmodified lines15 unmodified lines methods: retryMethods, statusCodes: retryStatusCodes, afterStatusCodes: retryAfterStatusCodes, maxRetryAfter: Number.POSITIVE_INFINITY, backoffLimit: Number.POSITIVE_INFINITY,};
export const normalizeRetryOptions = (retry: number | RetryOptions = {}): Required<RetryOptions> => { if (typeof retry === 'number') {20 unmodified linesmodified:test/retry.ts+59 added
1+import {performance, PerformanceObserver} from 'node:perf_hooks';2+import process from 'node:process';3 import test from 'ava';4 import ky from '../source/index.js';5 import {createHttpTestServer} from './helpers/create-http-test-server.js';6 434 unmodified lines441 });442 443 await server.close();444 });445+446+test('respect maximum backoff', async t => {447+ const retryCount = 5;448+ let requestCount = 0;449+450+ const server = await createHttpTestServer();451+ server.get('/', (_request, response) => {452+ requestCount++;453+454+ if (requestCount === retryCount) {455+ response.end(fixture);456+ } else {457+ response.sendStatus(500);458+ }459+ });460+461+ // We allow the test to take more time on CI than locally, to reduce flakiness462+ const allowedOffset = process.env.CI ? 1000 : 300;463+464+ // Register observer that asserts on duration when a measurement is performed465+ const obs = new PerformanceObserver(items => {466+ const measurements = items.getEntries();467+468+ 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;470+471+ t.true(Math.abs(duration - expectedDuration) < allowedOffset, `Duration of ${duration}ms is not close to expected duration ${expectedDuration}ms`); // Allow for 300ms difference472+473+ if (measurements[0].name === 'custom') {474+ obs.disconnect();475+ }476+ });477+ obs.observe({entryTypes: ['measure']});478+479+ // Start measuring480+ performance.mark('start');481+ t.is(await ky(server.url, {482+ retry: retryCount,483+ }).text(), fixture);484+ performance.mark('end');485+486+ performance.mark('start-custom');487+ requestCount = 0;488+ t.is(await ky(server.url, {489+ retry: {490+ limit: retryCount,491+ backoffLimit: 1000,492+ },493+ }).text(), fixture);494+495+ performance.mark('end-custom');496+497+ performance.measure('default', 'start', 'end');498+ performance.measure('custom', 'start-custom', 'end-custom');499+500+ await server.close();501+});modified:readme.md+6 added-2 removed
168 unmodified lines169 - `limit`: `2`170 - `methods`: `get` `put` `head` `delete` `options` `trace`171 - `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)172 - `maxRetryAfter`: `undefined`173+- `backoffLimit`: `undefined`174 175 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.176 177 If `retry` is a number, it will be used as `limit` and other defaults will remain in place.178 179 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.180 180-Delays between retries is calculated with the function `0.3 * (2 ** (retry - 1)) * 1000`, where `retry` is the attempt number (starts from 1).181+The `backoffLimit` option is the upper limit of the delay per retry in milliseconds.182+To clamp the delay, set `backoffLimit` to 1000, for example.183+By default, the delay is calculated with `0.3 * (2 ** (attemptCount - 1)) * 1000`. The delay increases exponentially.184 185 Retries are not triggered following a [timeout](#timeout).186 187 ```js2 unmodified lines190 const json = await ky('https://example.com', {191 retry: {192 limit: 10,193 methods: ['get'],191- statusCodes: [413]194+ statusCodes: [413],195+ backoffLimit: 3000196 }197 }).json();198 ```199 458 unmodified lines