Skip to main content

A ligthweight circuit breaker implementation for Python.

Project description

tnm-circuit-breaker

A lightweight Python circuit breaker for protecting downstream systems.

See USAGE for full examples.


Quick summary

  • Purpose: protect downstream services (i.e., Elasticsearch, RabbitMQ, Kafka, Postgres, external HTTP APIs, ...) from cascading failures.

  • Backends:

    • local: no extra dependencies needed
    • redis: distributed backend using Redis for atomic operations.

Install

# core package
pip install tnm-circuit-breaker

# redis support
pip install "tnm-circuit-breaker[redis]"

CircuitBreaker Public APIs

class CircuitBreaker:
    def record_failure(*args) -> int: ...

    def last_failure(*args): ...

    def record_success(*args) -> None: ...

    def protect(*args): ...

    # a decorator

    def execute(*args): ...

    async def execute_async(*args): ...

    def raise_circuit_open_error(*args): ...

Quick Usage

Using the protect decorator

from tnm.circuit import get_breaker
from tnm.circuit.exceptions import CircuitOpenError

breaker = get_breaker()  # defaults


@breaker.protect("service-name")
def my_function():
    pass


try:
    my_function()
except CircuitOpenError:
    ...
    # handle circuit open

Using the execute method

from tnm.circuit import get_breaker
from tnm.circuit.exceptions import CircuitOpenError

breaker = get_breaker()  # defaults


def my_function():
    pass


try:
    breaker.execute("service-name", my_function)
except CircuitOpenError:
    ...
    # handle circuit open

Using the execute_async method

import asyncio
from tnm.circuit import get_breaker
from tnm.circuit.exceptions import CircuitOpenError

breaker = get_breaker()  # defaults


async def my_async_function():
    pass


async def main():
    try:
        await breaker.execute_async("service-name", my_async_function)
    except CircuitOpenError:
        ...
        # handle circuit open


if __name__ == "__main__":
    asyncio.run(main())

Exceptions

All library exceptions inherit from a small, focused hierarchy:

  • CircuitError: Base exception for all circuit breaker errors.
  • CircuitBackendError: backend operational issues.
  • CircuitOpenError: raised when an operation is attempted while a service's circuit is open.
  • ReturnValuePolicyError: raised when a return-value rule matched; contains .retval and .retval_policy.

Handle CircuitOpenError or ReturnValuePolicyError as an expected operational outcome.

When catching, inspect .args or .__cause__ for low-level detail.


Contributing

Contributions welcome.


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

tnm_circuit_breaker-0.2.2.tar.gz (28.3 kB view details)

Uploaded Source

Built Distribution

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

tnm_circuit_breaker-0.2.2-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

Details for the file tnm_circuit_breaker-0.2.2.tar.gz.

File metadata

  • Download URL: tnm_circuit_breaker-0.2.2.tar.gz
  • Upload date:
  • Size: 28.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for tnm_circuit_breaker-0.2.2.tar.gz
Algorithm Hash digest
SHA256 a81db1711121f5e240aed966bb2daf1b42142cd7c1c1c1a10bdb2d3960967b9a
MD5 4d811ed02661e6da0026a165a9636495
BLAKE2b-256 13ef7ab8645ff78d320c100edc5acd782a9750c8b50dcfe1b7b2c1a41a0ee748

See more details on using hashes here.

File details

Details for the file tnm_circuit_breaker-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for tnm_circuit_breaker-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cccbc05dc62c2f663c4b51d3a60bb552b26ab9e833a9af1c741c8d09ee3e607b
MD5 53de0abf495b09836deb6bbfcc6b6767
BLAKE2b-256 3120c28e8d420dd87e0510625147b68d85b6405873b7cb339a00dc410e82631b

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