-
Notifications
You must be signed in to change notification settings - Fork 161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add delayFactor param to exponentialDelay #208
Conversation
es/index.mjs
Outdated
export function exponentialDelay(retryNumber = 0) { | ||
const delay = Math.pow(2, retryNumber) * 100; | ||
const delay = Math.pow(2, retryNumber) * 1000; | ||
const randomSum = delay * 0.2 * Math.random(); // 0-20% of the delay | ||
return delay + randomSum; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Made some updates to satisfy the latest Google recommendations for exponential backoff.
export function exponentialDelay(retryNumber = 0) {
const randomMs = Math.random() * 1000; // 0 to 1000 milliseconds
const exponentialDelay = Math.pow(2, retryNumber) * 1000 + randomMs;
const maxBackoff = 64 * 1000; // 64 seconds
const delay = Math.min(exponentialDelay, maxBackoff);
const deadline = 10 * 60 * 1000; // 10 minutes
if (delay > deadline) {
throw new Error(
`Exponential retry delay exhausted after ${
deadline / 1000 / 60
} minutes (${retryNumber} retries).`,
);
}
return delay;
}
We'd probably want to allow the user to configure the maxBackoff
and deadline
to their liking.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a good idea but deserves its own PR IMO.
I'd really love if we can make this configurable with a default value equal to the current static value (100) |
@mindhells please review my attempt at this 7845a5a thanks! |
tests got broken, not sure why 🤔 , would you have a look? |
* aligns with Google's "Exponential Backoff" link in README
* keep default of 100 to maintain existing behavior
Looks like |
Removing the error parameter from the retryDelay callback would break the library API (see Readme) |
This reverts commit 897dc6e.
Gotcha. Well it's kind of awkward since the error is not the first param of the callback. In the name of backwards compatibility this is the best I could come up with. Lmk your thoughts. |
LGTM, thanks a lot! |
Issue
Current implementation of
exponentialDelay
results in retries based on the order of 100ms – i.e. 100ms, 200ms, 400ms, 800ms, etc. This is in contrast with the Exponential Backoff link found in the README which suggests 1s, 2s, 4s, 8s, etc.Solution
Add
delayFactor
param which can be passed 1000. Defaults to 100 as to keep the current behavior.Related