API resilience library — exponential backoff and circuit breaker
Project description
smoothapi-py
API resilience library for Python. It provides a decorator to wrap your HTTP requests with exponential backoff, full jitter, and a finite-state machine circuit breaker to protect against cascading failures.
Zero dependencies. Fully typed. Supports both sync and async functions out of the box. Automatically integrates with requests and httpx if installed.
Install
pip install smoothapi-py
Features
- Exponential Backoff with Full Jitter: Prevents the "thundering herd" problem by randomizing retry delays.
- Circuit Breaker (FSM): Isolated state machine (
CLOSED→OPEN→HALF_OPEN) per decorated function. Thread-safe execution. - Smart Retries: Automatically detects HTTP status codes from
requestsandhttpxexceptions. Retries on transient codes (429, 500, 502, 503, 504) and re-raises client errors immediately. - Graceful Fallbacks: Optionally return cached or default data instantly when the circuit is
OPEN, bypassing network IO entirely.
Usage
Define your configuration once and apply the @resilient_api decorator to your functions:
from smooth_api import resilient_api, ResilientConfig
from smooth_api.config import BackoffConfig, CircuitBreakerConfig
import requests
config = ResilientConfig(
backoff=BackoffConfig(
base_delay=0.1, # seconds to wait before first retry
max_delay=30.0, # cap on exponential growth
max_retries=3 # max number of retry attempts
),
circuit_breaker=CircuitBreakerConfig(
failure_threshold=3, # trip OPEN after 3 consecutive failures
cooldown_ms=10_000 # stay OPEN for 10 seconds before probing
),
# Optional: return this exact object when the circuit is OPEN
fallback={"status": "degraded", "data": []},
# Optional: HTTP status codes to trigger a retry
retry_on=[429, 500, 502, 503, 504]
)
@resilient_api(config)
def get_user_data(user_id: str):
res = requests.get(f"https://api.example.com/users/{user_id}")
res.raise_for_status()
return res.json()
# Standard usage
try:
data = get_user_data("123")
print(data)
except Exception as e:
print("Request failed completely:", e)
# You can also override the fallback at runtime per-call:
data = get_user_data("456", fallback={"status": "override"})
Async Support
The decorator automatically detects if your function is a coroutine and uses asyncio.sleep instead of blocking the thread:
import httpx
@resilient_api(config)
async def get_user_data_async(user_id: str):
async with httpx.AsyncClient() as client:
res = await client.get(f"https://api.example.com/users/{user_id}")
res.raise_for_status()
return res.json()
How It Works
- Isolation: The circuit breaker state is isolated per decorated function (
fn.__qualname__). - Circuit Check: Before execution, the breaker checks the state. If it's
OPEN, the request is blocked instantly (returning your fallback, or raisingRuntimeError). - Execution & Retries: If an exception is raised, it attempts to extract the HTTP status code (supports
requestsandhttpx). If the status is inretry_on, it's counted as a failure and the thread sleeps with backoff. - Recovery: After
cooldown_ms, the breaker entersHALF_OPEN. The next execution acts as a probe. If it succeeds, the circuit closes. If it fails, it snaps back toOPENimmediately.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file smoothapi_py-0.1.0.tar.gz.
File metadata
- Download URL: smoothapi_py-0.1.0.tar.gz
- Upload date:
- Size: 6.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c9e5ed729d4c1dc4c27df24dc23adbbf1ccf64d42adf872c5c54bd80b0dcff7
|
|
| MD5 |
5e7d2cd0cb679a7e5026fc3d58a5452e
|
|
| BLAKE2b-256 |
509ab31c33af5818dad0bf27b21b6a64856cbdcee7afd99d4c423c07a2340a47
|
File details
Details for the file smoothapi_py-0.1.0-py3-none-any.whl.
File metadata
- Download URL: smoothapi_py-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
456cf8f41cb709bbcc136463f14da1830c1543cd46d8507fed32e21bd529cdc0
|
|
| MD5 |
fb100e1ff9a49c58fdd5d6dcf4674282
|
|
| BLAKE2b-256 |
446ef7e1ca7924e85af6ec56be3ba510b921065c7d36670d48f345b1fd04d0fb
|