[SDK-2687] Implement automatic rate-limit handling #285
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
RestClientOptions
object to carry a configuration throughout the Management calls of the SDK, fromAuth0
instantiation down through to theRestClient
calls. There was no existing way of gracefully applyingRestClient
options (telemetry
,timeouts
, and nowretries
) without having to write custom instantiation code. This can now all be done just from theAuth0
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 aRestClientOptions
is not provided toRestClient
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 usingRestClientOptions
directly.Auth0
constructor to acceptrest_options
argument for aRestClientOptions
configuration object. This is further passed on to the constructors for all the Management endpoint classes.Blacklists
,ClientGrants
,Clients
,Connections
,CustomDomains
,DeviceCredentials
,EmailTemplates
,Emails
,Grants
,Guardian
,Hooks
,Jobs
,LogStreams
,Logs
,Organizations
,ResourceServers
,Roles
,RulesConfigs
,Rules
,Stats
,Tenants
,Tickets
,UserBlocks
,UsersByEmail
, andUsers
constructors to acceptrest_options
argument for aRestClientOptions
configuration object.RestClient
constructor to acceptoptions
argument for aRestClientOptions
configuration object.RestClient.get()
with automatic retry logic.RestClient.MAX_REQUEST_RETRIES()
,MAX_REQUEST_RETRY_JITTER()
,MAX_REQUEST_RETRY_DELAY()
andMIN_REQUEST_RETRY_DELAY()
getters to read the hard limits from, to avoid using mutable values.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.RestClient._skip_sleep
to avoid actually calling sleep() during unit tests and greatly slowing down the test process.test_get_rate_limit_error
andtest_get_rate_limit_error_without_headers
, and addstest_get_rate_limit_custom_retries
,test_get_rate_limit_invalid_retries_below_min
,test_get_rate_limit_invalid_retries_above_max
andtest_get_rate_limit_retries_use_exponential_backoff
to unit tests to cover new functionality.References
Please see the internal SDK-2653 epic on this topic.
Testing
Tests can be executed using
python3 -m unittest -v
.Checklist