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

[SDK-2687] Implement automatic rate-limit handling #285

Merged
merged 6 commits into from
Aug 19, 2021

Conversation

evansims
Copy link
Member

@evansims evansims commented Aug 16, 2021

Changes

This PR enables support for automatic retry of API requests that encounter a 429 rate-limit status response from the Auth0 API. It will retry, by default, up to 3 times, with a hard-coded cap of 10. It exponentially increases the delay between each request ("exponential backoff"), and uses a jitter technique to slightly randomize the variance of this delay.

  • Adds a RestClientOptions object to carry a configuration throughout the Management calls of the SDK, from Auth0 instantiation down through to the RestClient calls. There was no existing way of gracefully applying RestClient options (telemetry, timeouts, and now retries) without having to write custom instantiation code. This can now all be done just from the Auth0 constructor.

Note: RestClientOptions is implemented in such a way as to provide this new cleaner configuration model without introducing breaking changes. All existing configuration arguments are still supported. If a RestClientOptions is not provided to RestClient during instantiation, one is created dynamically using any provided timeout or telemetry options passed as arguments. We should deprecate using the argument configurations in the future and transition developers toward using RestClientOptions directly.

  • Updates Auth0 constructor to accept rest_options argument for a RestClientOptions configuration object. This is further passed on to the constructors for all the Management endpoint classes.
  • Updates Blacklists, ClientGrants, Clients, Connections, CustomDomains, DeviceCredentials, EmailTemplates, Emails, Grants, Guardian, Hooks, Jobs, LogStreams, Logs, Organizations, ResourceServers, Roles, RulesConfigs, Rules, Stats, Tenants, Tickets, UserBlocks, UsersByEmail, and Users constructors to accept rest_options argument for a RestClientOptions configuration object.
  • Updates RestClient constructor to accept options argument for a RestClientOptions configuration object.
  • Updates RestClient.get() with automatic retry logic.
  • Adds RestClient.MAX_REQUEST_RETRIES(), MAX_REQUEST_RETRY_JITTER(), MAX_REQUEST_RETRY_DELAY() and MIN_REQUEST_RETRY_DELAY() getters to read the hard limits from, to avoid using mutable values.
  • Adds RestClient._metrics to aid in unit testing this new logic. This dict keeps track of the number of retry attempts, and the individual backoff windows applied to each retry.
  • Adds RestClient._skip_sleep to avoid actually calling sleep() during unit tests and greatly slowing down the test process.
  • Updates test_get_rate_limit_error and test_get_rate_limit_error_without_headers, and adds test_get_rate_limit_custom_retries, test_get_rate_limit_invalid_retries_below_min, test_get_rate_limit_invalid_retries_above_max and test_get_rate_limit_retries_use_exponential_backoff to unit tests to cover new functionality.
  • Updates other tests to prefer reading from the new internal configuration model (although technically they would work without changes until the old model is deprecated.)

References

Please see the internal SDK-2653 epic on this topic.

Testing

Tests can be executed using python3 -m unittest -v.

  • This change adds unit test coverage
  • This change adds integration test coverage
  • This change has been tested on the latest version of the platform/language or why not

Checklist

@evansims evansims added the review:small Small review label Aug 16, 2021
@evansims evansims added review:large Large review and removed review:small Small review labels Aug 17, 2021
@evansims evansims marked this pull request as ready for review August 17, 2021 18:07
@evansims evansims requested a review from a team as a code owner August 17, 2021 18:07
@jimmyjames jimmyjames added this to the v3-Next milestone Aug 19, 2021
Copy link
Contributor

@jimmyjames jimmyjames left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 LGTM, thanks for providing details on how this change is implemented to not introduce any breaking changes.

@evansims evansims merged commit f6925b6 into master Aug 19, 2021
@daschi
Copy link

daschi commented Aug 24, 2021

Thanks for adding this! I'm curious when the next release will be that will include the RestClientOptions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants