Circuit Breaker Middleware for Swarmauri
Project description
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.
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 swarmauri_middleware_circuitbreaker-0.7.0.tar.gz.
File metadata
- Download URL: swarmauri_middleware_circuitbreaker-0.7.0.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.27 {"installer":{"name":"uv","version":"0.9.27","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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5b4d5acf820135b1ce7834024859beb00c8ce59d24087f84f84f10f2b770d339
|
|
| MD5 |
1aeffa36b17a0748a44abc298cd0d7c6
|
|
| BLAKE2b-256 |
6a51599adaf340992eca90261c907fb538b8098eb3c833c94a85d181a8226a7e
|
File details
Details for the file swarmauri_middleware_circuitbreaker-0.7.0-py3-none-any.whl.
File metadata
- Download URL: swarmauri_middleware_circuitbreaker-0.7.0-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.27 {"installer":{"name":"uv","version":"0.9.27","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}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0e190fdc2a85dda7288ee49ce0e6c96b8ade6609536e255f1fc5e2196df610c8
|
|
| MD5 |
ffbfd97d2990882412000a467caf8f52
|
|
| BLAKE2b-256 |
991750f68d87f873853ab90f059ebd313a0758f878ec4f94115a5cdea0d31179
|