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.dev5.tar.gz.

File metadata

  • Download URL: swarmauri_middleware_circuitbreaker-0.7.3.dev5.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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.dev5.tar.gz
Algorithm Hash digest
SHA256 fd74aac8806c614f511b2a807f2cb63cb6739298b865404a914426cd3147e7f1
MD5 5d39ea52928b82bf94df82c16cf52b05
BLAKE2b-256 37d387ec6ae29f15454da9e63f848d17108ddf761f47b88b1753ddcf32db6ec5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swarmauri_middleware_circuitbreaker-0.7.3.dev5-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.4 {"installer":{"name":"uv","version":"0.10.4","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.dev5-py3-none-any.whl
Algorithm Hash digest
SHA256 5dd7d040ec5e499dd32fd6e701209555c9f32bc09e2b7c8cccb6164dc100ab4e
MD5 0c48d093e9cfcfc4b4c8a21cb31246d2
BLAKE2b-256 813b709abc68a7c6407b8826c2a0242cff2d1d118a314dfcf84d35efb82e7559

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