Skip to content

Commit

Permalink
Add retry.backoffLimit option
Browse files Browse the repository at this point in the history
Fixes #1599
  • Loading branch information
szmarczak committed Apr 15, 2021
1 parent 4f21eb3 commit 41c4136
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
10 changes: 8 additions & 2 deletions source/core/calculate-retry-delay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import type {RetryFunction} from './options';

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

const calculateRetryDelay: Returns<RetryFunction, number> = ({attemptCount, retryOptions, error, retryAfter, computedValue}) => {
const calculateRetryDelay: Returns<RetryFunction, number> = ({
attemptCount,
retryOptions,
error,
retryAfter,
computedValue
}) => {
if (error.name === 'RetryError') {
return 1;
}
Expand Down Expand Up @@ -34,7 +40,7 @@ const calculateRetryDelay: Returns<RetryFunction, number> = ({attemptCount, retr
}

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

export default calculateRetryDelay;
5 changes: 4 additions & 1 deletion source/core/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ export interface RetryOptions {
statusCodes: number[];
errorCodes: string[];
calculateDelay: RetryFunction;
backoffLimit: number;
maxRetryAfter?: number;
}

Expand Down Expand Up @@ -460,6 +461,7 @@ export interface PaginationOptions<ElementType, BodyType> {
@default 0
*/
backoff?: number;

/**
The maximum amount of request that should be triggered.
Retries on failure are not counted towards this limit.
Expand Down Expand Up @@ -616,7 +618,8 @@ const defaultInternals: Options['_internals'] = {
'EAI_AGAIN'
],
maxRetryAfter: undefined,
calculateDelay: ({computedValue}) => computedValue
calculateDelay: ({computedValue}) => computedValue,
backoffLimit: Number.POSITIVE_INFINITY
},
localAddress: undefined,
method: 'GET',
Expand Down
32 changes: 32 additions & 0 deletions test/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,3 +565,35 @@ test('reuses request options on retry', withServer, async (t, server, got) => {
t.is(retryCount, 1);
t.is(accept, 'application/json');
});

test('respects backoffLimit', withServer, async (t, server, got) => {
let count = 0;
let timestamp = Date.now();
const data: number[] = [];

server.get('/', (_request, response) => {
count++;

const now = Date.now();
data.push(now - timestamp);
timestamp = now;

if (count === 3) {
response.end(JSON.stringify(data));
} else {
response.statusCode = 408;
response.end();
}
});

const body = await got('', {
retry: {
backoffLimit: 0
}
}).json<number[]>();

t.is(body.length, 3);
t.true(body[0]! < 200);
t.true(body[1]! < 200);
t.true(body[2]! < 200);
});

0 comments on commit 41c4136

Please sign in to comment.