interlock
A modern circuit breaker for Python — sync and async in a single class, sliding-window rate and slow-call detection, a type-safe API, and transparent integrations at the transport level.
Installation
uv add interlock-cb # or: pip install interlock-cb
interlock-cb supports Python 3.11 and newer. The core uses only the standard library; external integrations are installed as optional extras.
Quickstart
Create one named breaker and reuse it around calls to the same dependency:
from interlock import CircuitBreaker, CircuitOpenError, Config
breaker = CircuitBreaker(
name='payments',
config=Config(failure_rate_threshold=0.5, minimum_number_of_calls=20),
)
@breaker
def charge(amount: int) -> str:
return gateway.charge(amount)
try:
receipt = charge(100)
except CircuitOpenError as exc:
print(exc)
The decorator preserves the function's signature and whether it is sync or
async. The same breaker also supports breaker.call(fn, ...), with breaker,
and async with breaker. See Getting started for all
calling styles and Configuration for every
threshold.
Why interlock
- Sync and async, one class.
CircuitBreakerdispatches to separate sync and async paths without duplicating the public API. - Failure rates over sliding windows. Choose count- or time-based windows instead of relying only on consecutive failures.
- Slow calls and returned values count. Detect latency degradation and classify unsuccessful results even when no exception is raised.
- Type-safe decorators. Wrapped signatures and their sync/async nature are
preserved; the package ships
py.typedand passes three strict type checkers. - Zero-dependency core. Optional clients, frameworks, storage and observability integrations never leak into the core package.
- Composable resilience. Combine timeout, bulkhead, breaker, retry and fallback explicitly, or coordinate breaker state across instances with Redis.
Safe production rollout
Start a new integration in METRICS_ONLY to observe real failure and slow-call
rates without rejecting traffic. The initial state is applied before a lazy
per-host breaker can admit its first request:
import httpx2
from interlock import Config, State
from interlock.integrations.httpx2 import AsyncCircuitBreakerTransport
transport = AsyncCircuitBreakerTransport(
httpx2.AsyncHTTPTransport(),
initial_state=State.METRICS_ONLY,
config=Config(failure_rate_threshold=0.25, minimum_number_of_calls=50),
listener=metrics_listener,
)
Use an EventListener for production metrics. For local diagnostics,
transport.registry.get_existing(host) returns an already-created breaker
without creating one, so its state and snapshot() can be inspected safely.
Hosts are only known at runtime, so transport.registry.items() lists every
breaker created so far — a point-in-time copy, name and breaker together.
After tuning thresholds, deploy a new transport with the default
initial_state=State.CLOSED; the enforcing instance starts with a fresh window.
See States and manual control.
Resilience pipeline
Compose strategies in an explicit order (first is outermost) while keeping the breaker useful as a standalone primitive:
from interlock import CircuitBreaker, CircuitOpenError, Pipeline
breaker = CircuitBreaker(name='recommendations')
pipeline = (
Pipeline.builder()
.fallback(lambda exc: [], on=(CircuitOpenError,))
.retry(attempts=4) # requires interlock-cb[tenacity]
.circuit_breaker(breaker)
.bulkhead(8)
.timeout(2.0)
.build()
)
@pipeline
async def fetch_picks(user: str) -> list[str]:
return await client.get_picks(user)
Retries never hammer an open circuit, one hung attempt cannot eat the retry budget, and every decision is observable — see the pipeline guide.
Integrations
The httpx2 transport applies one breaker per host with no decorators at call
sites:
import httpx2
from interlock.integrations.httpx2 import CircuitBreakerTransport
transport = CircuitBreakerTransport(httpx2.HTTPTransport())
client = httpx2.Client(transport=transport)
By default, transport exceptions and the canonical retryable statuses
(429, 500, 502, 503, 504) count as failures; 4xx client errors do not.
| Integration | Install | Documentation |
|---|---|---|
| httpx2 | interlock-cb[httpx2] |
Per-host transport |
| httpx | interlock-cb[httpx] |
Per-host transport |
| aiohttp | interlock-cb[aiohttp] |
Client middleware |
| requests | interlock-cb[requests] |
Session adapter |
| FastAPI | interlock-cb[fastapi] |
503 + Retry-After handler |
| Litestar | interlock-cb[litestar] |
503 + Retry-After handler |
| tenacity | interlock-cb[tenacity] |
Retry composition |
| Redis | interlock-cb[redis] |
Shared state |
| OpenTelemetry | interlock-cb[otel] |
Metrics listener |
The integrations overview also includes recipes for LLM SDKs and Flask/Django.
How it compares
interlock-cb is young: its first release was in 2026. Established libraries such as pybreaker and circuitbreaker have carried production traffic for years and remain a better fit when maturity matters more than the feature differences.
| Feature | interlock-cb | pybreaker | circuitbreaker |
|---|---|---|---|
| Core states (closed / open / half-open) | ✅ | ✅ | ✅ |
| Native asyncio | ✅ | Tornado | ✅ |
| Trip condition | failure rate | consecutive failures | consecutive failures |
| Time-based sliding window | ✅ | — | — |
| Slow-call detection | ✅ | — | — |
| Shared state across processes | ✅ | ✅ | — |
| Composable resilience pipeline | ✅ | — | — |
Fully typed API (py.typed) |
✅ | — | — |
The full comparison covers more features as well as aiobreaker and purgatory. Something out of date or unfair? Please open a PR.
The reliability work compensating for the project's shorter production history includes 100% branch coverage, three strict type checkers, mutation testing of the state machine and engine, property- and model-based tests, and CI on free-threaded CPython. The correctness and testing page documents what is verified and where the limits are.
Documentation
The full documentation is hosted at https://bagowix.github.io/interlock/. Start with:
- Getting started
- Configuration and states
- Resilience pipeline
- Integrations
- Correctness and testing
- API reference
For a deterministic, network-free demonstration of every state transition, run
the examples/ scripts or follow the walkthrough.
Contributing
Bug reports and pull requests are welcome. See
CONTRIBUTING.md for the local setup and the checks a change
must pass, and CODE_OF_CONDUCT.md for community
expectations. Security issues: please follow SECURITY.md.
License
interlock is released under the MIT License.
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 interlock_cb-2.6.0.tar.gz.
File metadata
- Download URL: interlock_cb-2.6.0.tar.gz
- Upload date:
- Size: 457.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1dec3be68f56d5dcf09e567dd688f7ded03ccdae19765386154e86d97c41180b
|
|
| MD5 |
c1d87fedd31d9a07115695cdbf0b093b
|
|
| BLAKE2b-256 |
dfbf4e1781f126d40da4d9b42b92e56d5681b112a7c52cfff3accd91ae7ee23f
|
Provenance
The following attestation bundles were made for interlock_cb-2.6.0.tar.gz:
Publisher:
release.yml on bagowix/interlock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
interlock_cb-2.6.0.tar.gz -
Subject digest:
1dec3be68f56d5dcf09e567dd688f7ded03ccdae19765386154e86d97c41180b - Sigstore transparency entry: 2439341784
- Sigstore integration time:
-
Permalink:
bagowix/interlock@92f1562dc20b51d53799e66dd5b9bbddda29093b -
Branch / Tag:
refs/tags/v2.6.0 - Owner: https://github.com/bagowix
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@92f1562dc20b51d53799e66dd5b9bbddda29093b -
Trigger Event:
push
-
Statement type:
File details
Details for the file interlock_cb-2.6.0-py3-none-any.whl.
File metadata
- Download URL: interlock_cb-2.6.0-py3-none-any.whl
- Upload date:
- Size: 81.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via:
twine/7.0.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e4a8458704b40b69b4cf8e4d12f534067f9e9ff7fa85d01a16292b91f8a729d1
|
|
| MD5 |
ca82788944b1470def4a9fedb6468b18
|
|
| BLAKE2b-256 |
43f5f20bef2b292a3a8c8afb6b68de8f3f77bff9b2e64db6f7bb4368fcf17ced
|
Provenance
The following attestation bundles were made for interlock_cb-2.6.0-py3-none-any.whl:
Publisher:
release.yml on bagowix/interlock
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
interlock_cb-2.6.0-py3-none-any.whl -
Subject digest:
e4a8458704b40b69b4cf8e4d12f534067f9e9ff7fa85d01a16292b91f8a729d1 - Sigstore transparency entry: 2439341813
- Sigstore integration time:
-
Permalink:
bagowix/interlock@92f1562dc20b51d53799e66dd5b9bbddda29093b -
Branch / Tag:
refs/tags/v2.6.0 - Owner: https://github.com/bagowix
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@92f1562dc20b51d53799e66dd5b9bbddda29093b -
Trigger Event:
push
-
Statement type: