A modern, composable circuit breaker library for Python
Project description
Fluxgate
A composable circuit breaker library for Python.
Installation
pip install fluxgate
Quick Start
from fluxgate import CircuitBreaker
cb = CircuitBreaker(name="my_api")
@cb
def call_api():
return requests.get("https://api.example.com")
That's it. The circuit breaker will:
- Track the last 100 calls
- Open when failure rate exceeds 50% (after 100+ requests)
- Wait 60 seconds before testing recovery
- Gradually allow more calls as the service recovers
Async Support
For async applications, use AsyncCircuitBreaker:
from fluxgate import AsyncCircuitBreaker
cb = AsyncCircuitBreaker(name="my_api")
@cb
async def call_api():
async with httpx.AsyncClient() as client:
return await client.get("https://api.example.com")
Customization
Every component is customizable. You can apply different policies to different services based on their criticality:
import httpx
from fluxgate import AsyncCircuitBreaker
from fluxgate.windows import TimeWindow
from fluxgate.trackers import TypeOf, Custom
from fluxgate.trippers import MinRequests, FailureRate, FailureStreak
from fluxgate.retries import Backoff
from fluxgate.permits import RampUp
# Track only 5xx errors and network failures (not 4xx client errors)
def is_server_error(e: Exception) -> bool:
if isinstance(e, httpx.HTTPStatusError):
return e.response.status_code >= 500
return isinstance(e, (httpx.ConnectError, httpx.TimeoutException))
# Conservative policy for critical payment service
payment_cb = AsyncCircuitBreaker(
name="payment_service",
window=TimeWindow(size=300), # Track calls over 5 minutes
tracker=Custom(is_server_error), # Custom error filtering
tripper=(
FailureStreak(5) | # Trip on 5 consecutive failures OR
(MinRequests(20) & FailureRate(0.4)) # 40% failure rate after 20 calls
),
retry=Backoff(initial=30.0, max_duration=600.0), # 30s, 60s, 120s... up to 10min
permit=RampUp(0.1, 1.0, 60.0), # Gradually allow 10% → 100% over 60s
)
# Aggressive policy for less critical inventory service
inventory_cb = AsyncCircuitBreaker(
name="inventory_service",
window=TimeWindow(size=60), # Track calls over 1 minute
tracker=TypeOf(httpx.HTTPError), # Track all HTTP errors
tripper=MinRequests(10) & FailureRate(0.6), # 60% failure rate after 10 calls
retry=Backoff(initial=10.0, max_duration=300.0),
)
@payment_cb
async def charge_payment(amount: float):
async with httpx.AsyncClient() as client:
response = await client.post("https://payment.example.com/charge", json={"amount": amount})
response.raise_for_status()
return response.json()
@inventory_cb
async def check_inventory(product_id: str):
async with httpx.AsyncClient() as client:
response = await client.get(f"https://inventory.example.com/products/{product_id}")
response.raise_for_status()
return response.json()
Components
| Component | Purpose | Examples |
|---|---|---|
| Window | How to track call history | CountWindow(100), TimeWindow(60) |
| Tracker | Which exceptions to track | All(), TypeOf(HTTPError), Custom(func) |
| Tripper | When to open the circuit | MinRequests(n), FailureRate(0.5), FailureStreak(5), SlowRate(0.5), AvgLatency(1.0), Closed(), HalfOpened() |
| Retry | When to attempt recovery | Cooldown(60.0), Backoff(initial=10.0), Always(), Never() |
| Permit | How to allow calls during recovery | Random(0.5), RampUp(0.1, 1.0, 60.0) |
Monitoring
Attach listeners to get notified on state transitions:
from fluxgate import CircuitBreaker
from fluxgate.listeners.log import LogListener
from fluxgate.listeners.prometheus import PrometheusListener
from fluxgate.listeners.slack import SlackListener
cb = CircuitBreaker(
name="my_api",
listeners=[
LogListener(),
PrometheusListener(),
SlackListener(channel="C123", token="xoxb-..."),
],
)
Documentation
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 fluxgate-0.4.1.tar.gz.
File metadata
- Download URL: fluxgate-0.4.1.tar.gz
- Upload date:
- Size: 89.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a0b8d800b7a254a454c2672ff218356d14a261e48edd78071dc51b56787c892c
|
|
| MD5 |
386749726ebc03d85ca344ae95377850
|
|
| BLAKE2b-256 |
99af53f89b7e62f2e35bcb9762acc3a9bd8d945f72095e430b4438b25be0f643
|
Provenance
The following attestation bundles were made for fluxgate-0.4.1.tar.gz:
Publisher:
publish.yml on byExist/fluxgate
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fluxgate-0.4.1.tar.gz -
Subject digest:
a0b8d800b7a254a454c2672ff218356d14a261e48edd78071dc51b56787c892c - Sigstore transparency entry: 764511514
- Sigstore integration time:
-
Permalink:
byExist/fluxgate@a2b4adae956419ca9fb07687fd22e4667f8bf985 -
Branch / Tag:
refs/tags/v0.4.1 - Owner: https://github.com/byExist
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a2b4adae956419ca9fb07687fd22e4667f8bf985 -
Trigger Event:
push
-
Statement type:
File details
Details for the file fluxgate-0.4.1-py3-none-any.whl.
File metadata
- Download URL: fluxgate-0.4.1-py3-none-any.whl
- Upload date:
- Size: 21.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7cf687d1407f8e2208d63f8456347db7a3bf0c358e7147e92589eac915e31bb7
|
|
| MD5 |
a6b27cd33ca9b686a24b477ceb2752c4
|
|
| BLAKE2b-256 |
b2a793f89b6ae6c7795d15b0cce30a80dfcf38c68528baae86c95022c6ef15c7
|
Provenance
The following attestation bundles were made for fluxgate-0.4.1-py3-none-any.whl:
Publisher:
publish.yml on byExist/fluxgate
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
fluxgate-0.4.1-py3-none-any.whl -
Subject digest:
7cf687d1407f8e2208d63f8456347db7a3bf0c358e7147e92589eac915e31bb7 - Sigstore transparency entry: 764511517
- Sigstore integration time:
-
Permalink:
byExist/fluxgate@a2b4adae956419ca9fb07687fd22e4667f8bf985 -
Branch / Tag:
refs/tags/v0.4.1 - Owner: https://github.com/byExist
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@a2b4adae956419ca9fb07687fd22e4667f8bf985 -
Trigger Event:
push
-
Statement type: