-
Notifications
You must be signed in to change notification settings - Fork 18
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: allow strategies other than polynomial #412
base: main
Are you sure you want to change the base?
Conversation
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.
Hey @galargh, thanks for the changes here ❤️ - I just had a few questions/thoughts on your change, let me know what you think!
src/error-request.ts
Outdated
let retryAfter; | ||
switch (state.strategy) { | ||
case "exponential": | ||
retryAfter = Math.pow(2, base); |
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.
You have base inverted with your power - You'll need to do:Math.pow(base, exp)
.
Also, you might want to wrap this in Math.round(Math.pow(base, exp))
just because pow
returns a double
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.
Thanks! I renamed base
variable to retryCount
so that it's a bit less confusing. I also wrapped it in Math.round
as suggested.
It reads as following now:
Math.round(Math.pow(2, retryCount))
As for the inversion, as per https://en.wikipedia.org/wiki/Exponential_backoff, exponential backoff is formed by taking the multiplicative factor (in this case 2) to the power of the number of observed events (in this case the number of retries that have already been performed). So I think it is correct now. Let me know if you wanted me to use different names though.
src/error-request.ts
Outdated
retryAfter = base; | ||
break; | ||
default: // "polynomial" | ||
retryAfter = Math.pow(base, 2); |
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.
For the polynomial strategy, you'll want Math.pow(2, base)
Also, you might want to wrap this in Math.round(Math.pow(base, exp))
just because pow
returns a double
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.
Wrapped Math.pow
with Math.round
- great spot, thanks 😄
As mentioned in #412 (comment), I think that would make it exponential backoff.
Full disclosure, I named this one polynomial by looking at what others tool call it (e.g. https://backoff-utils.readthedocs.io/en/latest/strategies.html#polynomial).
Resolves #ISSUE_NUMBER (not applicable; issue does not exist)
Behavior
Before the change?
RETRY_AFTER_BASE * (RETRY_COUNT + 1)^2
)After the change?
strategy
as an option.exponential
(RETRY_AFTER_BASE * 2^(RETRY_COUNT + 1)
)linear
(RETRY_AFTER_BASE * (RETRY_COUNT + 1)
)polynomial
(RETRY_AFTER_BASE * (RETRY_COUNT + 1)^2
)polynomial
strategy (backwards compatible).Other information
Additional info
Pull request checklist
Does this introduce a breaking change?
Please see our docs on breaking changes to help!
Type: Breaking change
label)If
Yes
, what's the impact:Pull request type
Type: Feature