feat: implement built-in retry mechanism with exponential backoff and jitter#21
Open
Deadpool2000 wants to merge 5 commits into
Open
feat: implement built-in retry mechanism with exponential backoff and jitter#21Deadpool2000 wants to merge 5 commits into
Deadpool2000 wants to merge 5 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Ref issue #20
This PR resolves the lack of a built-in retry mechanism in the SDK client and OAuth client interfaces. Previously, any temporary network dropout, gateway timeout (502/503/504), or rate limit hit (429) would cause immediate request failures and require custom retry handling in the consumer's application.
This change introduces a robust, configurable retry mechanism using exponential backoff and randomized jitter to retry failed requests automatically.
The Problem
httpx) had no automatic retry loop.Retry-Afterheaders.The Solution
I implemented an optional, fully backward-compatible retry mechanism across all client classes:
ClientAsyncClientOauthClientAsyncOauthClientmax_retries(defaults to0for backward compatibility),backoff_factor, andretry_on_status._request_with_retrymethod (both sync and async versions) executes the network calls.httpx.RequestError) and configured status codes (defaults to[429, 502, 503, 504]).Retry-Afterheader, the wait time is dynamically overridden to respect the header.Key File Changes
openapi_python_sdk/client.py: Updates to synchronousClientwith sync retry loop.openapi_python_sdk/async_client.py: Updates to asynchronousAsyncClientusingasyncio.sleepretry loop.openapi_python_sdk/oauth_client.py: Updates to syncOauthClientwrapping credentials and token operations.openapi_python_sdk/async_oauth_client.py: Updates to asyncAsyncOauthClientwrapping credentials and token operations.tests/test_retry.py(New): Full unit test coverage for sync retries (testing backoff delays, rate-limit headers, and failures).tests/test_async_retry.py(New): Full unit test coverage for async retries.How I tested it
poetry run pytestand verified all 35 tests passed successfully (including existing mocks).poetry run ruff check .to verify code format styling.httpbin.orgtesting endpoints to verify that the client retries over multiple attempts and waits exponentially.