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

File metadata

  • Download URL: swarmauri_middleware_circuitbreaker-0.7.3.dev3.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.3.dev3.tar.gz
Algorithm Hash digest
SHA256 cea5749902e28712b704ff595f98c5805c551cb1fe75b0daf1605f4d80739383
MD5 73ab520af7153e9c621871de7302f160
BLAKE2b-256 bb8f933b9cbe2b2e3474d857f77b9e1635eca459f217577ad298392e64d274d9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swarmauri_middleware_circuitbreaker-0.7.3.dev3-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.3.dev3-py3-none-any.whl
Algorithm Hash digest
SHA256 bb41f841db972d4aa9e50ad1be3bfc1720d64e3c12aef81e102a3710ce735e2c
MD5 13a75377c4451082ed828aa073b8fab2
BLAKE2b-256 ce8767229c3392fca37818339194251dc973bf51a54e87afcb0f9b457748d3be

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