pybreaker: Python implementation of the Circuit Breaker pattern. #755
Labels
Algorithms
Sorting, Learning or Classifying. All algorithms go here.
Automation
Automate the things
Code-Interpreter
OpenAI Code-Interpreter
programming-languages
Topics related to programming languages and their features.
python
Python code, tools, info
source-code
Code snippets
danielfm/pybreaker: Python implementation of the Circuit Breaker pattern.
PyBreaker
PyBreaker is a Python implementation of the Circuit Breaker pattern, described in Michael T. Nygard's book Release It!.
In Nygard's words, "circuit breakers exists to allow one subsystem to fail without destroying the entire system. This is done by wrapping dangerous operations (typically integration points) with a component that can circumvent calls when the system is not healthy".
Features
Requirements
Installation
Run the following command line to download the latest stable version of PyBreaker from PyPI:
If you are a Git user, you might want to install the current development version in editable mode:
Usage
The first step is to create an instance of CircuitBreaker for each integration point you want to protect against:
CircuitBreaker instances should live globally inside the application scope, e.g., live across requests.
Note: Integration points to external services (i.e. databases, queues, etc) are more likely to fail, so make sure to always use timeouts when accessing such services if there's support at the API level.
If you'd like to use the Redis backing, initialize the CircuitBreaker with a CircuitRedisStorage:
Note: Do not initialize the Redis connection with the decode_responses set to True, this will force returning ASCII objects from redis and in Python3+ will fail with:
Note: You may want to reuse a connection already created in your application, if you're using django_redis for example:
Note: If you require multiple, independent CircuitBreakers and wish to store their states in Redis, it is essential to assign a unique namespace for each CircuitBreaker instance. This can be achieved by specifying a distinct namespace parameter in the CircuitRedisStorage constructor. for example:
Event Listening
There's no need to subclass CircuitBreaker if you just want to take action when certain events occur. In that case, it's better to subclass CircuitBreakerListener instead:
To add listeners to a circuit breaker:
What Does a Circuit Breaker Do?
Let's say you want to use a circuit breaker on a function that updates a row in the customer database table:
Or if you don't want to use the decorator syntax:
Or use it as a context manager and a with statement:
According to the default parameters, the circuit breaker db_breaker will automatically open the circuit after 5 consecutive failures in update_customer.
When the circuit is open, all calls to update_customer will fail immediately (raising CircuitBreakerError) without any attempt to execute the real operation. If you want the original error to be thrown when the circuit trips, set the throw_new_error_on_trip option to False:
After 60 seconds, the circuit breaker will allow the next call to update_customer pass through. If that call succeeds, the circuit is closed; if it fails, however, the circuit is opened again until another timeout elapses.
Optional Tornado Support
A circuit breaker can (optionally) be used to call asynchronous Tornado functions:
Or if you don't want to use the decorator syntax:
Excluding Exceptions
By default, a failed call is any call that raises an exception. However, it's common to raise exceptions to also indicate business exceptions, and those exceptions should be ignored by the circuit breaker as they don't indicate system errors:
In that case, when any function guarded by that circuit breaker raises CustomerValidationError (or any exception derived from CustomerValidationError), that call won't be considered a system failure.
So as to cover cases where the exception class alone is not enough to determine whether it represents a system error, you may also pass a callable rather than a type:
You may mix types and filter callables freely.
Monitoring and Management
A circuit breaker provides properties and functions you can use to monitor and change its current state:
These properties and functions might and should be exposed to the operations staff somehow as they help them to detect problems in the system.
Contributing
Run tests:
Code formatting (black and isort) and linting (mypy)
Above commands will automatically install the necessary tools inside .pyprojectx and also install pre-commit hooks.
List available commands:
Suggested labels
The text was updated successfully, but these errors were encountered: