Skip to content

Commit 41c4136

Browse files
committed
Add retry.backoffLimit option
Fixes #1599
1 parent 4f21eb3 commit 41c4136

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

source/core/calculate-retry-delay.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import type {RetryFunction} from './options';
22

33
type Returns<T extends (...args: any) => unknown, V> = (...args: Parameters<T>) => V;
44

5-
const calculateRetryDelay: Returns<RetryFunction, number> = ({attemptCount, retryOptions, error, retryAfter, computedValue}) => {
5+
const calculateRetryDelay: Returns<RetryFunction, number> = ({
6+
attemptCount,
7+
retryOptions,
8+
error,
9+
retryAfter,
10+
computedValue
11+
}) => {
612
if (error.name === 'RetryError') {
713
return 1;
814
}
@@ -34,7 +40,7 @@ const calculateRetryDelay: Returns<RetryFunction, number> = ({attemptCount, retr
3440
}
3541

3642
const noise = Math.random() * 100;
37-
return ((2 ** (attemptCount - 1)) * 1000) + noise;
43+
return Math.min(((2 ** (attemptCount - 1)) * 1000), retryOptions.backoffLimit) + noise;
3844
};
3945

4046
export default calculateRetryDelay;

source/core/options.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ export interface RetryOptions {
293293
statusCodes: number[];
294294
errorCodes: string[];
295295
calculateDelay: RetryFunction;
296+
backoffLimit: number;
296297
maxRetryAfter?: number;
297298
}
298299

@@ -460,6 +461,7 @@ export interface PaginationOptions<ElementType, BodyType> {
460461
@default 0
461462
*/
462463
backoff?: number;
464+
463465
/**
464466
The maximum amount of request that should be triggered.
465467
Retries on failure are not counted towards this limit.
@@ -616,7 +618,8 @@ const defaultInternals: Options['_internals'] = {
616618
'EAI_AGAIN'
617619
],
618620
maxRetryAfter: undefined,
619-
calculateDelay: ({computedValue}) => computedValue
621+
calculateDelay: ({computedValue}) => computedValue,
622+
backoffLimit: Number.POSITIVE_INFINITY
620623
},
621624
localAddress: undefined,
622625
method: 'GET',

test/retry.ts

+32
Original file line numberDiff line numberDiff line change
@@ -565,3 +565,35 @@ test('reuses request options on retry', withServer, async (t, server, got) => {
565565
t.is(retryCount, 1);
566566
t.is(accept, 'application/json');
567567
});
568+
569+
test('respects backoffLimit', withServer, async (t, server, got) => {
570+
let count = 0;
571+
let timestamp = Date.now();
572+
const data: number[] = [];
573+
574+
server.get('/', (_request, response) => {
575+
count++;
576+
577+
const now = Date.now();
578+
data.push(now - timestamp);
579+
timestamp = now;
580+
581+
if (count === 3) {
582+
response.end(JSON.stringify(data));
583+
} else {
584+
response.statusCode = 408;
585+
response.end();
586+
}
587+
});
588+
589+
const body = await got('', {
590+
retry: {
591+
backoffLimit: 0
592+
}
593+
}).json<number[]>();
594+
595+
t.is(body.length, 3);
596+
t.true(body[0]! < 200);
597+
t.true(body[1]! < 200);
598+
t.true(body[2]! < 200);
599+
});

0 commit comments

Comments
 (0)