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.2.dev7.tar.gz.

File metadata

  • Download URL: swarmauri_middleware_circuitbreaker-0.7.2.dev7.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","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.2.dev7.tar.gz
Algorithm Hash digest
SHA256 fcbdfa65a438d7e5c621c353c8195128b9e6c68f85b854ffc4ea49ed9865bdea
MD5 ee808a652b2ab505f20f99ee1f428a65
BLAKE2b-256 65645afd127b407b9febe33096bab07a8f295b0c3a91027cb4f247fc1c30c93a

See more details on using hashes here.

File details

Details for the file swarmauri_middleware_circuitbreaker-0.7.2.dev7-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_middleware_circuitbreaker-0.7.2.dev7-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.3 {"installer":{"name":"uv","version":"0.10.3","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.2.dev7-py3-none-any.whl
Algorithm Hash digest
SHA256 fb50fd10d4cbe3bf6ce96dcc513d3feb73aa1f2f56bde3063859afa80cbe5421
MD5 48c13b3ac206a4b839d8bdb0c8e4f547
BLAKE2b-256 b09fcd06580508455b6221b171ed8ef33f1e613400a7cc7118a5d7241d13b1d8

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