Skip to content

Commit

Permalink
fix: decorrelatedJitter backoff returning NaN after many iterations
Browse files Browse the repository at this point in the history
Closes #86
  • Loading branch information
connor4312 committed May 4, 2024
1 parent 03614ab commit 74b3c78
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 3.1.3

- **fix:** decorrelatedJitter backoff returning NaN after many iterations ([#86](https://github.com/connor4312/cockatiel/issues/86))

## 3.1.2

- **chore:** remove test files from dist package ([#84](https://github.com/connor4312/cockatiel/issues/84))
Expand Down
17 changes: 17 additions & 0 deletions src/backoff/ExponentialBackoffGenerators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,21 @@ describe('ExponentialBackoff Generators', () => {
}
});
}

it('fixes issue #86', () => {
const options: IExponentialBackoffOptions<any> = {
generator: decorrelatedJitterGenerator,
maxDelay: 15 * 60_000,
exponent: 2,
initialDelay: 45_000,
};

let state: any;
for (let k = 1; k < 3000; k++) {
const [delay, nextState] = decorrelatedJitterGenerator(state, options);
expect(delay).to.not.be.NaN;
expect(delay).to.be.gte(0);
state = nextState;
}
});
});
2 changes: 1 addition & 1 deletion src/backoff/ExponentialBackoffGenerators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const decorrelatedJitterGenerator: GeneratorFn<[number, number]> = (state
const [attempt, prev] = state || [0, 0];
const t = attempt + Math.random();
const next = Math.pow(options.exponent, t) * Math.tanh(Math.sqrt(pFactor * t));
const formulaIntrinsicValue = Math.max(0, next - prev);
const formulaIntrinsicValue = isFinite(next) ? Math.max(0, next - prev) : Infinity;
return [
Math.min(formulaIntrinsicValue * rpScalingFactor * options.initialDelay, options.maxDelay),
[attempt + 1, next],
Expand Down

0 comments on commit 74b3c78

Please sign in to comment.