Skip to main content

Async circuit breaker with optional Redis (coredis) support for distributed state

Project description

ss-utils-circuit-breaker

A Python async circuit breaker implementation with optional Redis (coredis) support for distributed state.

Installation

# Basic installation (in-memory state only)
uv add ss-utils-circuit-breaker

# With Redis support
uv add ss-utils-circuit-breaker[redis]

Quick Start

Basic Usage (In-Memory)

from ss_utils_circuit_breaker import CircuitBreaker, CircuitBreakerError

# Create a circuit breaker
breaker = CircuitBreaker(
    name="my_api",
    failure_threshold=5,  # Open after 5 failures
    recovery_timeout=60,  # Try again after 60 seconds
)

@breaker
async def call_external_api():
    # Your async API call here
    async with httpx.AsyncClient() as client:
        response = await client.get("https://api.example.com/data")
        response.raise_for_status()
        return response.json()

# Usage
try:
    data = await call_external_api()
except CircuitBreakerError:
    # Circuit is open - service is known to be unavailable
    data = get_cached_data()
except Exception:
    # Regular failure
    pass

With Redis (Distributed State)

from ss_utils_circuit_breaker import RedisCircuitBreaker

# Create a Redis-backed circuit breaker
breaker = RedisCircuitBreaker(
    name="my_api",
    redis_url="localhost:6379",
    namespace="circuit_breaker",  # Redis key prefix
    failure_threshold=5,
    recovery_timeout=60,
)

@breaker
async def call_external_api():
    # State is shared across all instances via Redis
    pass

Circuit Breaker States

CLOSED (Normal)
    │
    ▼ (failures >= threshold)
   OPEN (Blocking)
    │
    ▼ (after recovery_timeout)
HALF_OPEN (Testing)
    │
    ├─► (success) ──► CLOSED
    │
    └─► (failure) ──► OPEN
  • CLOSED: Normal operation. Calls pass through, failures are counted.
  • OPEN: Circuit is tripped. Calls fail immediately with CircuitBreakerError.
  • HALF_OPEN: After timeout, one test call is allowed. Success closes circuit, failure reopens it.

Configuration Example

import httpx
from ss_utils_circuit_breaker import CircuitBreaker

# Only count connection errors as failures
api_breaker = CircuitBreaker(
    name="external_api",
    failure_threshold=3,
    recovery_timeout=30,
    expected_exception=(httpx.ConnectError, httpx.TimeoutException),
)

@api_breaker
async def fetch_data():
    async with httpx.AsyncClient(timeout=10.0) as client:
        return await client.get("https://api.example.com/data")

Testing

# Run tests
uv run pytest

# With coverage
uv run pytest --cov

License

MIT

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

ss_utils_circuit_breaker-0.0.1.tar.gz (109.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ss_utils_circuit_breaker-0.0.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file ss_utils_circuit_breaker-0.0.1.tar.gz.

File metadata

  • Download URL: ss_utils_circuit_breaker-0.0.1.tar.gz
  • Upload date:
  • Size: 109.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ss_utils_circuit_breaker-0.0.1.tar.gz
Algorithm Hash digest
SHA256 45e234fd5a0a0c52435e48ef043e83ab5e95f591bad63fdfb318e904ef1d07a2
MD5 5ba86aa85ffc912d648fcce8918a8b9d
BLAKE2b-256 29fbbc1da3a28525163e153a9bde65fc8f8237714e31c5401f7b136dc05feb64

See more details on using hashes here.

File details

Details for the file ss_utils_circuit_breaker-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: ss_utils_circuit_breaker-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ss_utils_circuit_breaker-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2c9abe86505a99cb526474d9cece49451d19063a57b66267e301a4361ce3e387
MD5 dacb9f04c37562f70efad572577b446c
BLAKE2b-256 83e48dbdce043874c5dc623e075d9ad4264c6daaab99a0024f2937d7a4dae4ea

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