Swarmauri Middleware Circuitbreaker
FastAPI middleware that adds a configurable circuit breaker (powered by pybreaker) to Swarmauri services. Automatically blocks requests after repeated failures and re-opens based on a half-open probation window.
Features
- Integrates with Swarmauri's
MiddlewareBaseinterface—drop in viaapp.add_middleware. - Configurable thresholds:
fail_max,reset_timeout, andhalf_open_wait_time. - Supports async FastAPI request handling and conveys 429 responses when the circuit is open.
- Logs state transitions (closed ➜ open ➜ half-open) for observability.
Prerequisites
- Python 3.10 or newer.
- FastAPI application (ASGI) using Swarmauri's middleware system.
pybreakerinstalled (included as a dependency of this package).
Installation
# pip
pip install swarmauri_middleware_circuitbreaker
# poetry
poetry add swarmauri_middleware_circuitbreaker
# uv (pyproject-based projects)
uv add swarmauri_middleware_circuitbreaker
Quickstart
from fastapi import FastAPI
from swarmauri_middleware_circuitbreaker import CircuitBreakerMiddleware
app = FastAPI()
app.add_middleware(
CircuitBreakerMiddleware,
fail_max=5,
reset_timeout=30,
half_open_wait_time=10,
)
@app.get("/unstable")
async def unstable_endpoint():
raise RuntimeError("Simulated failure")
- After 5 failures (
fail_max=5), the circuit opens and subsequent calls receive HTTP 429. - After
reset_timeoutseconds, a single test request is allowed in the half-open state; success closes the circuit, failure keeps it open.
Observing the Circuit
import logging
logging.basicConfig(level=logging.INFO)
# Logs include:
# "Circuit half-open: Waiting for test request to determine health"
# "Circuit opened: Excessive failures detected"
# "Circuit closed: Service is healthy again"
Integrate with your logging/monitoring stack to alert on circuit state changes.
Tips
- Use targeted middleware stacks; wrap only the routes that call upstream services prone to failure.
- Tune
fail_maxandreset_timeoutfor each dependency—critical paths may require conservative thresholds. - Pair with retry logic or queueing to degrade gracefully while the circuit is open.
- When testing locally, trigger failures intentionally to ensure your observability tracks circuit transitions correctly.
Want to help?
If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.
Metadata
Release files for swarmauri_middleware_circuitbreaker 0.7.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| swarmauri_middleware_circuitbreaker-0.7.3.tar.gz | 7.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| swarmauri_middleware_circuitbreaker-0.7.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.6 kB
Release files / swarmauri_middleware_circuitbreaker-0.7.3.tar.gz
| Download URL | swarmauri_middleware_circuitbreaker-0.7.3.tar.gz |
|---|---|
| Size | 7.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a45a210a609a74ca6ac2565524fda507d7e411e38e52e2195f7fce8b5f7944b0
|
|
BLAKE2b-256 checksum How to use checksums |
40b35ca7e41cd8cd589143004a5ba4f8c5f9a3f59a1a75d260ac5cf3862b70af
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / swarmauri_middleware_circuitbreaker-0.7.3-py3-none-any.whl
| Download URL | swarmauri_middleware_circuitbreaker-0.7.3-py3-none-any.whl |
|---|---|
| Size | 8.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
772a185a968e94a41b1f3c613990e118e2ab12ac07ed00f3dce6f45afd1978df
|
|
BLAKE2b-256 checksum How to use checksums |
be197dcaccb9298825fae35822712fa70dcf87e1b8718104fdf68e454e54423b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.0 {"installer":{"name":"uv","version":"0.11.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|