A modern, composable circuit breaker library for Python
Project description
Fluxgate
A composable circuit breaker library for Python with first-class sync and async support.
English | 한국어
Why Fluxgate?
Circuit breakers prevent cascading failures by monitoring service health and temporarily blocking calls to failing dependencies. Most Python libraries trip on consecutive failures — brittle for any service with intermittent errors. Fluxgate trips on failure rates over a sliding window, and rules are first-class values you compose:
from fluxgate import CircuitBreaker
from fluxgate.trippers import MinRequests, FailureRate, SlowRate, FailureStreak
cb = CircuitBreaker(
tripper=FailureStreak(5) | (MinRequests(20) & (
FailureRate(0.5) | SlowRate(0.3, threshold=1.0)
)),
)
Note: State is process-local and not thread-safe. For concurrency, use
asyncio+AsyncCircuitBreaker, not threading.
Installation
pip install fluxgate # core, zero dependencies
pip install "fluxgate[prometheus]" # +PrometheusListener
pip install "fluxgate[slack]" # +SlackListener
Usage
import httpx
from fluxgate import AsyncCircuitBreaker
from fluxgate.trackers import TypeOf
cb = AsyncCircuitBreaker(
tracker=TypeOf(httpx.ConnectError, httpx.TimeoutException),
max_half_open_calls=10,
)
@cb
async def fetch(url):
async with httpx.AsyncClient() as client:
return (await client.get(url)).json()
CircuitBreaker mirrors this API for sync code. A tripped circuit raises CallNotPermittedError; pass @cb(fallback=...) to fall back gracefully.
How It Works
Fluxgate is a state machine. The core cycle is CLOSED → OPEN → HALF_OPEN:
stateDiagram-v2
direction LR
[*] --> CLOSED
CLOSED --> OPEN: tripper
OPEN --> HALF_OPEN: retry
HALF_OPEN --> CLOSED: !tripper
HALF_OPEN --> OPEN: tripper
Three additional states (METRICS_ONLY, DISABLED, FORCED_OPEN) are documented in Operational Controls.
Composable Rules
Every condition is a value and combines with & / |:
from fluxgate.trippers import (
Closed, HalfOpened, MinRequests, FailureRate, SlowRate, FailureStreak,
)
# State-specific rules: stricter when probing recovery.
tripper = FailureStreak(5) | (MinRequests(20) & (
(Closed() & (FailureRate(0.5) | SlowRate(0.3, threshold=1.0))) |
(HalfOpened() & (FailureRate(0.3) | SlowRate(0.2, threshold=1.0)))
))
Tracker (failure classification) follows the same pattern with & / | / ~.
Components
| Component | Role | Examples |
|---|---|---|
Window |
Track recent calls (count- or time-based) | CountWindow(100), TimeWindow(60) |
Tracker |
Classify which exceptions count as failures | All(), TypeOf(HTTPError), Custom(fn); combine with & / | / ~ |
Tripper |
Decide when to open the circuit | MinRequests, FailureRate, SlowRate, AvgLatency, FailureStreak, Closed/HalfOpened; combine with & / | |
Retry |
Trigger OPEN → HALF_OPEN |
Cooldown, Backoff, Always, Never |
Permit |
Admit calls in HALF_OPEN |
All, Random(p), RampUp(start, end, duration) |
Listener |
React to state transitions | LogListener, PrometheusListener, SlackListener |
All components are abstract base classes (abc.ABC) with input validation — misconfigurations fail fast at construction time. Subclass to write your own.
Operational Controls
Beyond automatic trips, the breaker exposes hooks for safe rollouts and manual control:
cb.metrics_only()— shadow mode: collect metrics without ever tripping. Ideal for validating thresholds in production before going live.cb.force_open()/cb.disable()— manual override during incident response or maintenance.cb.info()— snapshot of state, metrics, and reopen count.cb.reset()— return to CLOSED and clear metrics.
Monitoring
Pass listeners via listeners=... — built-in: LogListener, PrometheusListener (optional), SlackListener (optional). Each listener takes a name= identifier used in log lines, Prometheus labels, or Slack messages. State transitions emit Signal events; AsyncCircuitBreaker additionally accepts AsyncListener.
Documentation
- Full documentation — concepts, components, examples, API reference
- Comparison with other libraries — vs
pybreaker,circuitbreaker,aiobreaker - Changelog — version history and migration guides
Development
uv sync --all-extras --all-groups
uv run pytest
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.9.4.tar.gz.
File metadata
- Download URL: fluxgate-0.9.4.tar.gz
- Upload date:
- Size: 107.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
799812a3c31fff47411cbd56b8958e21e8b9c1d529a9d715fd99d808d5b4ab9e
|
|
| MD5 |
cdfe1cb5c64390c9e0a914eb7d7fc7c2
|
|
| BLAKE2b-256 |
3355867589221469186e8b139375c74a1f36ef1c52b66a3e10ac3e5c3dd491bb
|
Provenance
The following attestation bundles were made for fluxgate-0.9.4.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.9.4.tar.gz -
Subject digest:
799812a3c31fff47411cbd56b8958e21e8b9c1d529a9d715fd99d808d5b4ab9e - Sigstore transparency entry: 1704584338
- Sigstore integration time:
-
Permalink:
byExist/fluxgate@049668194e560239d1d76faf94ff2d09f513bedc -
Branch / Tag:
refs/tags/v0.9.4 - Owner: https://github.com/byExist
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@049668194e560239d1d76faf94ff2d09f513bedc -
Trigger Event:
push
-
Statement type:
File details
Details for the file fluxgate-0.9.4-py3-none-any.whl.
File metadata
- Download URL: fluxgate-0.9.4-py3-none-any.whl
- Upload date:
- Size: 25.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6fb3e027dfc614e7908a3c798e5724ac3fc73547300e740ca195f3a3f9a2a33
|
|
| MD5 |
76588b94ecbc191925a8eb6ef234a0df
|
|
| BLAKE2b-256 |
819627c24fcf391c0c79efac42dd40613b3cfcde5523ac92dccdda5a81c85832
|
Provenance
The following attestation bundles were made for fluxgate-0.9.4-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.9.4-py3-none-any.whl -
Subject digest:
f6fb3e027dfc614e7908a3c798e5724ac3fc73547300e740ca195f3a3f9a2a33 - Sigstore transparency entry: 1704584369
- Sigstore integration time:
-
Permalink:
byExist/fluxgate@049668194e560239d1d76faf94ff2d09f513bedc -
Branch / Tag:
refs/tags/v0.9.4 - Owner: https://github.com/byExist
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@049668194e560239d1d76faf94ff2d09f513bedc -
Trigger Event:
push
-
Statement type: