Skip to main content

Circuit Breaker Middleware for Swarmauri

Project description

Swarmauri Logo

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


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 MiddlewareBase interface—drop in via app.add_middleware.
  • Configurable thresholds: fail_max, reset_timeout, and half_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.
  • pybreaker installed (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_timeout seconds, 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_max and reset_timeout for 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


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.7.3.dev22.tar.gz.

File metadata

  • Download URL: swarmauri_middleware_circuitbreaker-0.7.3.dev22.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","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.7.3.dev22.tar.gz
Algorithm Hash digest
SHA256 6ad13f3d6d20f67e004ba18aab0ee69d8cc98dcae5295359242a346246059c78
MD5 f608f052d4b84d128ce750d9e01072bf
BLAKE2b-256 d47fb4a22ef02a4fd107a189890518ee969e46feed71ee031a78d2f1fa948546

See more details on using hashes here.

File details

Details for the file swarmauri_middleware_circuitbreaker-0.7.3.dev22-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_middleware_circuitbreaker-0.7.3.dev22-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","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.7.3.dev22-py3-none-any.whl
Algorithm Hash digest
SHA256 b44f74c1c6b69fdef78c60e402815e742601d629963cf5e2cfd1d195bf687dbb
MD5 bb96bbd95b960851afbb010612ac40a8
BLAKE2b-256 7823531cb778432879e1388a300e72f2f7211e20642290dbf3a8c6a47a45d896

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