Skip to content

Commit

Permalink
fix: retry jitter
Browse files Browse the repository at this point in the history
  • Loading branch information
pichlermarc committed Jun 21, 2024
1 parent 767eedc commit f1577c0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ const MAX_ATTEMPTS = 5;
const INITIAL_BACKOFF = 1000;
const MAX_BACKOFF = 5000;
const BACKOFF_MULTIPLIER = 1.5;
const JITTER = 0.2;

/**
* Get a pseudo-random jitter that falls in the range of [-JITTER, +JITTER]
*/
function getJitter() {
return Math.random() * (2 * JITTER) - JITTER;
}

class RetryingTransport implements IExporterTransport {
constructor(private _transport: IExporterTransport) {}
Expand All @@ -38,11 +46,9 @@ class RetryingTransport implements IExporterTransport {
let attempts = MAX_ATTEMPTS;
let nextBackoff = INITIAL_BACKOFF;

// TODO: I'm not 100% sure this is correct, please review in-depth.
while (result.status === 'retryable' && attempts > 0) {
attempts--;
const upperBound = Math.min(nextBackoff, MAX_BACKOFF);
const backoff = Math.random() * upperBound;
const backoff = Math.min(nextBackoff, MAX_BACKOFF) + getJitter();
nextBackoff = nextBackoff * BACKOFF_MULTIPLIER;
result = await this.retry(data, result.retryInMillis ?? backoff);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ describe('RetryingTransport', function () {

it('does retry 5 times, then resolves as retryable', async function () {
// arrange
// make random return low values so that it does not actually need to wait long for the backoff.
Math.random = sinon.stub().returns(0.001);
// make random return a negative value so that what's passed to setTimeout() is negative and therefore gets executed immediately.
Math.random = sinon.stub().returns(-Infinity);

const retryResponse: ExportResponse = {
status: 'retryable',
Expand Down

0 comments on commit f1577c0

Please sign in to comment.