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.1.tar.gz (27.6 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.1-py3-none-any.whl (22.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tnm_circuit_breaker-0.2.1.tar.gz
  • Upload date:
  • Size: 27.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for tnm_circuit_breaker-0.2.1.tar.gz
Algorithm Hash digest
SHA256 9bea260e7d8008301eae32a957ed300d25003f049d977b49f74be762bde342a2
MD5 90dea2c0466bd747f422f9d5ed1c86bc
BLAKE2b-256 532856bd5e00016b0a607d72fe430a9b59c6f3d31d351695e9fc8f1394bc2818

See more details on using hashes here.

Provenance

The following attestation bundles were made for tnm_circuit_breaker-0.2.1.tar.gz:

Publisher: publish-to-pypi.yml on mpamba/tnm-circuit-breaker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for tnm_circuit_breaker-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2861bcba46062b2ebd00c16ba93ba9d65afd683f006fa62fdaa51f455ae303ee
MD5 6cd0d3950d5d962285b7f0665e684fb6
BLAKE2b-256 c92a8a84dced02b922f496675451c2cea98dd8f207e21253af41701fbf012df8

See more details on using hashes here.

Provenance

The following attestation bundles were made for tnm_circuit_breaker-0.2.1-py3-none-any.whl:

Publisher: publish-to-pypi.yml on mpamba/tnm-circuit-breaker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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