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

File metadata

  • Download URL: swarmauri_middleware_circuitbreaker-0.7.3.dev21.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.dev21.tar.gz
Algorithm Hash digest
SHA256 b6ea2b1d87ad79d54920facfff7b588988e3fec78dc88914b7162ba24147f534
MD5 f0ac911920c1fc44a6b2db74c6e5b175
BLAKE2b-256 a972b949df0f61a9d49adc4d7e18c3876cf6677b050edfce9ff4bb3be42ce058

See more details on using hashes here.

File details

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

File metadata

  • Download URL: swarmauri_middleware_circuitbreaker-0.7.3.dev21-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.dev21-py3-none-any.whl
Algorithm Hash digest
SHA256 1cbd28cbf1a8de1a30ea9a86714b9670ca1f2c3780cb68e915813b416de39625
MD5 b42846be6dd7fa62a8c9887bb8aaf378
BLAKE2b-256 b8551ebc5f3507d9c728e3ca29c32b86ab2b3e7a5749889ccadb3299f7a27cf8

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