openapi-python-sdk icon indicating copy to clipboard operation
openapi-python-sdk copied to clipboard

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

Open Deadpool2000 opened this issue 5 months ago • 0 comments

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.

Deadpool2000 avatar Jun 07 '26 17:06 Deadpool2000