Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

galargh
Copy link

@galargh galargh commented Mar 8, 2023

Resolves #ISSUE_NUMBER (not applicable; issue does not exist)


Behavior

Before the change?

  • The plugin always performs retries using polynomial strategy (RETRY_AFTER_BASE * (RETRY_COUNT + 1)^2)

After the change?

  • The plugin accepts strategy as an option.
  • The accepted values are
    • exponential (RETRY_AFTER_BASE * 2^(RETRY_COUNT + 1))
    • linear (RETRY_AFTER_BASE * (RETRY_COUNT + 1))
    • polynomial (RETRY_AFTER_BASE * (RETRY_COUNT + 1)^2)
  • If no strategy is provided, the plugin uses polynomial strategy (backwards compatible).
  • The plugins throws an error if an invalid strategy name is provided.

Other information

  • not applicable

Additional info

Pull request checklist

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)
  • Added the appropriate label for the given change

Does this introduce a breaking change?

Please see our docs on breaking changes to help!

  • Yes (Please add the Type: Breaking change label)
  • No (I think it should fall under Adding optional parameters to existing SDK APIs)

If Yes, what's the impact:

  • N/A

Pull request type

  • Feature/model/API additions: Type: Feature

@wolfy1339 wolfy1339 added the Type: Feature New feature or request label Mar 8, 2023
Copy link
Contributor

@nickfloyd nickfloyd left a 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!

let retryAfter;
switch (state.strategy) {
case "exponential":
retryAfter = Math.pow(2, base);
Copy link
Contributor

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

Copy link
Author

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.

retryAfter = base;
break;
default: // "polynomial"
retryAfter = Math.pow(base, 2);
Copy link
Contributor

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

Copy link
Author

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Feature New feature or request
Projects
Status: 🏗 In progress
Development

Successfully merging this pull request may close these issues.

3 participants