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 neededredis: 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.retvaland.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
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a81db1711121f5e240aed966bb2daf1b42142cd7c1c1c1a10bdb2d3960967b9a
|
|
| MD5 |
4d811ed02661e6da0026a165a9636495
|
|
| BLAKE2b-256 |
13ef7ab8645ff78d320c100edc5acd782a9750c8b50dcfe1b7b2c1a41a0ee748
|
File details
Details for the file tnm_circuit_breaker-0.2.2-py3-none-any.whl.
File metadata
- Download URL: tnm_circuit_breaker-0.2.2-py3-none-any.whl
- Upload date:
- Size: 22.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cccbc05dc62c2f663c4b51d3a60bb552b26ab9e833a9af1c741c8d09ee3e607b
|
|
| MD5 |
53de0abf495b09836deb6bbfcc6b6767
|
|
| BLAKE2b-256 |
3120c28e8d420dd87e0510625147b68d85b6405873b7cb339a00dc410e82631b
|