Skip to main content

Async circuit-breaker middleware for Swarmauri and FastAPI request flows backed by pybreaker.

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_middleware_circuitbreaker Discord

Swarmauri Middleware Circuit Breaker

swarmauri_middleware_circuitbreaker is the Swarmauri middleware component for protecting unstable request paths with a circuit-breaker policy backed by pybreaker. It is designed for FastAPI-style request middleware and prevents repeated failures from repeatedly hitting unhealthy downstream services.

Why Use Swarmauri Middleware Circuit Breaker

  • Stop sending traffic to failing upstream dependencies after repeated errors.
  • Recover gradually by allowing a single half-open probe request before fully reopening traffic.
  • Add resilience controls to Swarmauri or FastAPI request flows without writing custom breaker logic.
  • Pair with retry and observability middleware to build layered failure protection.

FAQ

What parameters does this middleware expose?
fail_max, reset_timeout, and half_open_wait_time.

What happens when the circuit is open?
The middleware raises HTTP 429 for additional requests until a half-open probe is allowed.

Is this middleware async?
Yes. The main dispatch(request, call_next) entry point is asynchronous.

What request type does it expect?
It is written for fastapi.Request / Starlette-style HTTP request handling.

Features

  • Circuit-breaker protection using pybreaker.
  • Async dispatch(request, call_next) middleware surface.
  • Configurable failure threshold and reset timing.
  • Logging for open, half-open, and closed transitions.
  • Supports Python 3.10, 3.11, 3.12, 3.13, and 3.14.

Installation

uv add swarmauri_middleware_circuitbreaker
pip install swarmauri_middleware_circuitbreaker

Usage

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,
)

Examples

Protect an unstable endpoint

from fastapi import FastAPI
from swarmauri_middleware_circuitbreaker import CircuitBreakerMiddleware

app = FastAPI()
app.add_middleware(CircuitBreakerMiddleware, fail_max=5, reset_timeout=30)

@app.get("/unstable")
async def unstable():
    raise RuntimeError("Simulated failure")

Observe circuit transitions in logs

import logging

logging.basicConfig(level=logging.INFO)

# Expected messages include:
# "Circuit half-open: Waiting for test request to determine health"
# "Circuit opened: Excessive failures detected"
# "Circuit closed: Service is healthy again"

Understand open and recovery flow

# Repeated failures increment the internal counter.
# Once the threshold is exceeded, the circuit opens.
# A later request is allowed as a half-open probe.
# Success closes the circuit; failure reopens protection.

Related Packages

Swarmauri Foundations

More Documentation

Best Practices

  • Use targeted circuit breakers around unstable upstream dependencies rather than globally around every route.
  • Tune fail_max and reset_timeout per dependency instead of reusing one threshold everywhere.
  • Pair circuit breaking with retries carefully so one layer does not amplify the other.
  • Verify your monitoring captures circuit state changes before relying on the breaker in production.

License

This project is licensed under the Apache-2.0 License.

Project details


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

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file swarmauri_middleware_circuitbreaker-0.11.0.dev1.tar.gz.

File metadata

  • Download URL: swarmauri_middleware_circuitbreaker-0.11.0.dev1.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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

Hashes for swarmauri_middleware_circuitbreaker-0.11.0.dev1.tar.gz
Algorithm Hash digest
SHA256 796f33d73251cdad7f85872d4551c0538b83c04d82b386fc007d7a2bc2e533de
MD5 fc27260ebde34cc329ae69dff6f45a54
BLAKE2b-256 44325f2251440c4070c0b9acc0e5e612dbd66781ecfb692a58537ab43da6c2ee

See more details on using hashes here.

File details

Details for the file swarmauri_middleware_circuitbreaker-0.11.0.dev1-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_middleware_circuitbreaker-0.11.0.dev1-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","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

Hashes for swarmauri_middleware_circuitbreaker-0.11.0.dev1-py3-none-any.whl
Algorithm Hash digest
SHA256 b513b16067ca57b2e9f3e64ad81056746f1ba00ff366ad95830216d8efb70ff6
MD5 0765cfce38a459a7c90d9b9eea1834c7
BLAKE2b-256 254a54508e1afc9a5f65abf34e22746d2c6196d436c22f0f7b28aaa0d87507c1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page