Skip to content

fix: implement exponential backoff retry for transient network errors#400

Open
Bedirhan3428 wants to merge 1 commit into
getlago:mainfrom
Bedirhan3428:fix/transient-network-retry-logic
Open

fix: implement exponential backoff retry for transient network errors#400
Bedirhan3428 wants to merge 1 commit into
getlago:mainfrom
Bedirhan3428:fix/transient-network-retry-logic

Conversation

@Bedirhan3428

Copy link
Copy Markdown

Description

This PR enhances the reliability of the lago-python-client network layer by introducing transient network and server-side error resilience with an Exponential Backoff retry mechanism inside services/request.py.

Why is this necessary?

Currently, the _create_retry_wrapper specifically handles HTTP 429 (Rate Limit) errors. However, if a transient network anomaly occurs (such as a temporary socket dropping, DNS timeout, or edge proxies returning 502 Bad Gateway, 503 Service Unavailable, or 504 Gateway Timeout), the SDK crashes instantly. For critical production telemetry and financial invoicing, these temporary drops should be safely handled with immediate, non-blocking exponential retries before giving up.

What changes were made?

  1. Transient Server Errors Hook: Explicitly caught status codes 502, 503, and 504, raising a properly formed httpx.HTTPStatusError.
  2. Exponential Backoff Isolation: Wrapped the underlying network executions in a try/except block to catch httpx.RequestError and httpx.TimeoutException alongside server failures.
  3. Resiliency Loop: Implemented a max-retry limit of 3 network attempts, starting with a base delay of 1.0s and doubling sequentially (1s -> 2s -> 4s) using time.sleep().
  4. State Preservation: Synchronized both network_retry_attempt and the pre-existing SDK retry_attempt state to maintain absolute tracking accuracy for subsequent observability steps (emit_rate_limit_info).
  5. No Breaking Changes: Left the original HTTP 429 workflow completely intact.

Tested locally against the entire test suite, running flawlessly with 414 tests passing.

@cla-check-bot

cla-check-bot Bot commented Jun 8, 2026

Copy link
Copy Markdown

Welcome, @Bedirhan3428!

Thanks for your first contribution!

Before we proceed with the review, please sign the Fiduciary License Agreement:

Sign the FLA

Once signed, this PR will be automatically updated.

@cla-check-bot

cla-check-bot Bot commented Jun 8, 2026

Copy link
Copy Markdown

Thanks, @Bedirhan3428! 🎉

Your CLA has been signed and is now on file. We'll proceed with the review shortly.

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