Skip to content
Myr

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

5 files+83 added−3 removed

modified:source/core/Ky.ts+1 added-1 removed
233 unmodified lines
234 }235 }236237 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 }240241 return 0;242 }
119 unmodified lines
modified:source/types/retry.ts+16 added
modified:source/utils/normalize.ts+1 added
modified:test/retry.ts+59 added
1import {performance, PerformanceObserver} from 'node:perf_hooks';2import process from 'node:process';3import test from 'ava';4import ky from '../source/index.js';5import {createHttpTestServer} from './helpers/create-http-test-server.js';6
434 unmodified lines
441 });442443 await server.close();444});445446test('respect maximum backoff', async t => {447 const retryCount = 5;448 let requestCount = 0;449450 const server = await createHttpTestServer();451 server.get('/', (_request, response) => {452 requestCount++;453454 if (requestCount === retryCount) {455 response.end(fixture);456 } else {457 response.sendStatus(500);458 }459 });460461 // We allow the test to take more time on CI than locally, to reduce flakiness462 const allowedOffset = process.env.CI ? 1000 : 300;463464 // Register observer that asserts on duration when a measurement is performed465 const obs = new PerformanceObserver(items => {466 const measurements = items.getEntries();467468 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;470471 t.true(Math.abs(duration - expectedDuration) < allowedOffset, `Duration of ${duration}ms is not close to expected duration ${expectedDuration}ms`); // Allow for 300ms difference472473 if (measurements[0].name === 'custom') {474 obs.disconnect();475 }476 });477 obs.observe({entryTypes: ['measure']});478479 // 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,492 },493 }).text(), fixture);494495 performance.mark('end-custom');496497 performance.measure('default', 'start', 'end');498 performance.measure('custom', 'start-custom', 'end-custom');499500 await server.close();501});
modified:readme.md+6 added-2 removed
168 unmodified lines
169- `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`174175An 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.176177If `retry` is a number, it will be used as `limit` and other defaults will remain in place.178179If `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.180180Delays between retries is calculated with the function `0.3 * (2 ** (retry - 1)) * 1000`, where `retry` is the attempt number (starts from 1).181The `backoffLimit` option is the upper limit of the delay per retry in milliseconds.182To clamp the delay, set `backoffLimit` to 1000, for example.183By default, the delay is calculated with `0.3 * (2 ** (attemptCount - 1)) * 1000`. The delay increases exponentially.184185Retries are not triggered following a [timeout](#timeout).186187```js
2 unmodified lines
190const 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