API protection library — exponential backoff and circuit breaker
Project description
smoothapi-py
API protection 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 retryable 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. - Request Deduplication: Automatically merges concurrent identical requests into a single network call (async only).
Usage
Basic Usage (Defaults)
If you don't need custom configurations, you can use the decorator with its defaults by passing an empty config object.
import requests
from smooth_api import smooth_api, SmoothConfig
# Create it with default settings
config = SmoothConfig()
@smooth_api(config)
def get_user_data(user_id: str):
res = requests.get(f"https://api.example.com/users/{user_id}")
res.raise_for_status() # Always raise so the decorator knows it failed!
return res.json()
# Standard usage
try:
data = get_user_data("123")
print(data)
except Exception as e:
print("Request failed completely:", e)
Default Settings provided automatically:
- Retries: 3 attempts
- Backoff Base Delay: 0.1 seconds (100 milliseconds)
- Circuit Failure Threshold: Trips after 3 consecutive failures
- Circuit Cooldown: Stays open for 10 seconds before probing
- Status Codes to Retry:
429,500,502,503, and504
Advanced Usage (Custom Settings)
You can override any of the defaults to suit your application's needs, such as adding a fallback object.
from smooth_api import smooth_api, SmoothConfig
from smooth_api.config import BackoffConfig, CircuitBreakerConfig
import requests
config = SmoothConfig(
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]
)
@smooth_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()
# You can also override the fallback at runtime per-call:
data = get_user_data("456", fallback={"status": "override"})
Client Error Handling & Warnings
By default, non-retryable client errors (e.g. 400, 401, 403, 404, 405) bubble up and raise exceptions immediately. If you want to intercept these client errors:
from smooth_api import smooth_api, SmoothConfig
def my_callback(status: int, message: str):
print(f"Error hook: Received client error {status}")
config = SmoothConfig(
fallback_on_non_retryable=True,
# Optional: Custom error hook function
on_non_retryable_error=my_callback,
# Optional: Fallback returned on non-retryable errors
fallback={"status": "error", "message": "Not Found"}
)
- Default Warning: If
fallback_on_non_retryableisTrueand no customon_non_retryable_erroris defined, it will write a warning message tosys.stderr. - Graceful Return: If no
fallbackis configured, it returns a mockResponsewrapper withstatus_code,.json()returning{"error": True, "status": status, "message": "..."}, and.okreturningFalse. Code downstream can checkres.status_codeor callres.json()without raising exceptions.
Async Support
The decorator automatically detects if your function is a coroutine and uses asyncio.sleep instead of blocking the thread:
import httpx
@smooth_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()
Request Deduplication (Async Only)
When multiple identical async requests are made concurrently, SmoothAPI can execute only one network call and share the result with all callers. This reduces unnecessary load on downstream services.
Enable with default key function (deduplicates by positional args):
import httpx
from smooth_api import smooth_api, SmoothConfig
from smooth_api.config import DeduplicationConfig
config = SmoothConfig(deduplication=DeduplicationConfig())
@smooth_api(config)
async def get_user(user_id: int):
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()
# All three calls share a single network request
import asyncio
results = await asyncio.gather(
get_user(1),
get_user(1),
get_user(1),
)
Custom key function for advanced coalescing:
from smooth_api.config import DeduplicationConfig
def my_key(*args, **kwargs):
# Deduplicate by second argument (resource type)
return str(args[1]) if len(args) > 1 else str(args[0])
config = SmoothConfig(deduplication=DeduplicationConfig(key_fn=my_key))
Opt out of deduplication for specific calls:
config = SmoothConfig(
deduplication=DeduplicationConfig(
key_fn=lambda *a, **k: None # Return None to skip dedup
)
)
- Default Behavior: Deduplicates by joining positional args with
:. E.g.,get_user(1)produces key"1". - Error Propagation: If the coroutine raises an exception, all waiting callers receive the same exception.
- Settlement: Once a call completes, the next call with the same key triggers a fresh execution.
- Sync Functions: Deduplication only works with async-decorated functions. Sync functions are unaffected.
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. - Memory Management: The circuit breaker cache is capped at 1,000 domains. When exceeded,
CLOSEDcircuits with zero failures are swept to prevent memory leaks in dynamic environments.
License
MIT
Project details
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-1.2.2.tar.gz.
File metadata
- Download URL: smoothapi_py-1.2.2.tar.gz
- Upload date:
- Size: 103.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1780d8cfb01bc2c50de4f0365946000efbde14d368e6bf825108849b9e3e8b7b
|
|
| MD5 |
a64ec9b8ced8234121fa653ab615b3a2
|
|
| BLAKE2b-256 |
05dbfa739857428cda49361b58944683937def9b6b1f71ab8a5c3561e70145d4
|
File details
Details for the file smoothapi_py-1.2.2-py3-none-any.whl.
File metadata
- Download URL: smoothapi_py-1.2.2-py3-none-any.whl
- Upload date:
- Size: 11.5 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 |
8aa6fc41bada4d52bd6449ea891a4e4ba6ac6ea5354ac1b015e2793b0b170089
|
|
| MD5 |
6e42a7b5576f84d1f61bb2f1e8225b87
|
|
| BLAKE2b-256 |
1beeb0f7644a3c899f50042834749f5ea984c88eab36d5edbf1565d09810b587
|