Skip to content

feat: implement built-in retry mechanism with exponential backoff and jitter#21

Open
Deadpool2000 wants to merge 5 commits into
openapi:mainfrom
Deadpool2000:fr-20
Open

feat: implement built-in retry mechanism with exponential backoff and jitter#21
Deadpool2000 wants to merge 5 commits into
openapi:mainfrom
Deadpool2000:fr-20

Conversation

@Deadpool2000
Copy link
Copy Markdown
Contributor

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

  • Low-level HTTP primitives (via httpx) had no automatic retry loop.
  • Transient exceptions (like connection dropouts or request timeouts) crashed consumer applications instantly.
  • Rate-limiting (HTTP 429) was not handled automatically, ignoring Retry-After headers.

The Solution

I implemented an optional, fully backward-compatible retry mechanism across all client classes:

  1. Added Retry Parameters to:
    • Client
    • AsyncClient
    • OauthClient
    • AsyncOauthClient
    • Parameters: max_retries (defaults to 0 for backward compatibility), backoff_factor, and retry_on_status.
  2. Created Retry Loops:
    • _request_with_retry method (both sync and async versions) executes the network calls.
    • Retries trigger on connection/timeout errors (httpx.RequestError) and configured status codes (defaults to [429, 502, 503, 504]).
    • If an HTTP 429 response contains a Retry-After header, the wait time is dynamically overridden to respect the header.
  3. Randomized Jitter: Added random noise to backoff calculations to avoid thundering herd issues on the Openapi gateway.

Key File Changes

  • openapi_python_sdk/client.py: Updates to synchronous Client with sync retry loop.
  • openapi_python_sdk/async_client.py: Updates to asynchronous AsyncClient using asyncio.sleep retry loop.
  • openapi_python_sdk/oauth_client.py: Updates to sync OauthClient wrapping credentials and token operations.
  • openapi_python_sdk/async_oauth_client.py: Updates to async AsyncOauthClient wrapping 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

  1. Unit Tests:
    • Ran poetry run pytest and verified all 35 tests passed successfully (including existing mocks).
  2. Linter Check:
    • Ran poetry run ruff check . to verify code format styling.
  3. Integration Verification:
    • Tested live connections against httpbin.org testing endpoints to verify that the client retries over multiple attempts and waits exponentially.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant