From e83007704670b5f567ca338c156441f6c569ecb5 Mon Sep 17 00:00:00 2001 From: Szymon Marczak <36894700+szmarczak@users.noreply.github.com> Date: Tue, 27 Apr 2021 01:28:23 +0200 Subject: [PATCH] Add `noise` retry option Fixes #1690 --- source/core/calculate-retry-delay.ts | 2 +- source/core/options.ts | 9 ++++- test/arguments.ts | 50 ++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/source/core/calculate-retry-delay.ts b/source/core/calculate-retry-delay.ts index 1ace16d40..3f13b81fe 100644 --- a/source/core/calculate-retry-delay.ts +++ b/source/core/calculate-retry-delay.ts @@ -39,7 +39,7 @@ const calculateRetryDelay: Returns = ({ } } - const noise = Math.random() * 100; + const noise = Math.random() * retryOptions.noise; return Math.min(((2 ** (attemptCount - 1)) * 1000), retryOptions.backoffLimit) + noise; }; diff --git a/source/core/options.ts b/source/core/options.ts index 3abdc3ee4..7374d02c9 100644 --- a/source/core/options.ts +++ b/source/core/options.ts @@ -295,6 +295,7 @@ export interface RetryOptions { errorCodes: string[]; calculateDelay: RetryFunction; backoffLimit: number; + noise: number; maxRetryAfter?: number; } @@ -618,7 +619,8 @@ const defaultInternals: Options['_internals'] = { ], maxRetryAfter: undefined, calculateDelay: ({computedValue}) => computedValue, - backoffLimit: Number.POSITIVE_INFINITY + backoffLimit: Number.POSITIVE_INFINITY, + noise: 100 }, localAddress: undefined, method: 'GET', @@ -1785,6 +1787,11 @@ export default class Options { assert.any([is.array, is.undefined], value.methods); assert.any([is.array, is.undefined], value.statusCodes); assert.any([is.array, is.undefined], value.errorCodes); + assert.any([is.number, is.undefined], value.noise); + + if (value.noise && Math.abs(value.noise) > 100) { + throw new Error(`The maximum acceptable retry noise is +/- 100ms, got ${value.noise}`); + } for (const key in value) { if (!(key in this._internals.retry)) { diff --git a/test/arguments.ts b/test/arguments.ts index d59c835b8..ac0910bc3 100644 --- a/test/arguments.ts +++ b/test/arguments.ts @@ -557,3 +557,53 @@ test('prefixUrl is properly replaced when extending', withServer, async (t, serv t.is(await child.get('').text(), '/other/path/'); }); + +test('throws on too large noise', t => { + t.throws(() => { + new Options({ + retry: { + noise: 101 + } + }); + }, { + message: 'The maximum acceptable retry noise is +/- 100ms, got 101' + }); + + t.throws(() => { + new Options({ + retry: { + noise: -101 + } + }); + }, { + message: 'The maximum acceptable retry noise is +/- 100ms, got -101' + }); + + t.throws(() => { + new Options({ + retry: { + noise: Number.POSITIVE_INFINITY + } + }); + }, { + message: 'The maximum acceptable retry noise is +/- 100ms, got Infinity' + }); + + t.throws(() => { + new Options({ + retry: { + noise: Number.NEGATIVE_INFINITY + } + }); + }, { + message: 'The maximum acceptable retry noise is +/- 100ms, got -Infinity' + }); + + t.notThrows(() => { + new Options({ + retry: { + noise: 0 + } + }); + }); +});