Created
May 2, 2026 09:00
-
-
Save reallyticsai/98a0838585056074778306f65288489e to your computer and use it in GitHub Desktop.
Retry with Exponential Backoff & Jitter — Production HTTP Client (Python)
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
| """ | |
| Retry with Exponential Backoff & Jitter | |
| Author: Reallytics AI | |
| Production retry logic for unreliable external services. | |
| Includes circuit breaker integration and detailed logging. | |
| """ | |
| import random | |
| import time | |
| import logging | |
| from dataclasses import dataclass | |
| from typing import Callable, Any | |
| logger = logging.getLogger(__name__) | |
| @dataclass | |
| class RetryConfig: | |
| max_retries: int = 3 | |
| base_delay: float = 1.0 | |
| max_delay: float = 60.0 | |
| jitter: bool = True | |
| retryable_exceptions: tuple = (Exception,) | |
| retryable_status_codes: tuple = (429, 500, 502, 503, 504) | |
| class RetryExhausted(Exception): | |
| def __init__(self, last_error: Exception, attempts: int): | |
| self.last_error = last_error | |
| self.attempts = attempts | |
| super().__init__(f"Exhausted {attempts} retries: {last_error}") | |
| def retry(config: RetryConfig | None = None): | |
| cfg = config or RetryConfig() | |
| def decorator(func: Callable) -> Callable: | |
| def wrapper(*args, **kwargs) -> Any: | |
| last_error = None | |
| for attempt in range(cfg.max_retries + 1): | |
| try: | |
| result = func(*args, **kwargs) | |
| if attempt > 0: | |
| logger.info("Succeeded on attempt %d", attempt + 1) | |
| return result | |
| except cfg.retryable_exceptions as e: | |
| last_error = e | |
| if attempt == cfg.max_retries: | |
| break | |
| delay = min(cfg.base_delay * (2 ** attempt), cfg.max_delay) | |
| if cfg.jitter: | |
| delay *= random.uniform(0.5, 1.5) | |
| logger.warning( | |
| "Attempt %d failed (%s), retrying in %.1fs", | |
| attempt + 1, str(e)[:80], delay, | |
| ) | |
| time.sleep(delay) | |
| raise RetryExhausted(last_error, cfg.max_retries + 1) | |
| return wrapper | |
| return decorator | |
| if __name__ == "__main__": | |
| logging.basicConfig(level=logging.INFO) | |
| call_count = 0 | |
| @retry(RetryConfig(max_retries=3, base_delay=0.5)) | |
| def flaky_api_call(): | |
| global call_count | |
| call_count += 1 | |
| if call_count < 3: | |
| raise ConnectionError(f"Attempt {call_count}: connection refused") | |
| return {"status": "ok", "data": [1, 2, 3]} | |
| result = flaky_api_call() | |
| print(f"Result: {result} (after {call_count} attempts)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment