File: README.rst

package info (click to toggle)
python-circuitbreaker 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 132 kB
  • sloc: python: 504; makefile: 3
file content (166 lines) | stat: -rw-r--r-- 6,766 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
CircuitBreaker
--------------

.. image:: https://badge.fury.io/py/circuitbreaker.svg
    :target: https://badge.fury.io/py/circuitbreaker

.. image:: https://github.com/fabfuel/circuitbreaker/actions/workflows/build.yml/badge.svg
    :target: https://github.com/fabfuel/circuitbreaker/actions/workflows/build.yml

This is a Python implementation of the "Circuit Breaker" Pattern (https://martinfowler.com/bliki/CircuitBreaker.html).
Inspired by Michael T. Nygard's highly recommendable book *Release It!* (https://pragprog.com/titles/mnee2/release-it-second-edition/).


Installation
------------

The project is available on PyPI. Simply run::

    $ pip install circuitbreaker


Usage
-----

This is the simplest example. Just decorate a function with the ``@circuit`` decorator::

    from circuitbreaker import circuit

    @circuit
    def external_call():
        ...


This decorator sets up a circuit breaker with the default settings. The circuit breaker:

- monitors the function execution and counts failures
- resets the failure count after every successful execution (while it is closed)
- opens and prevents further executions after 5 subsequent failures
- switches to half-open and allows one test-execution after 30 seconds recovery timeout
- closes if the test-execution succeeded
- considers all raised exceptions (based on class ``Exception``) as an expected failure
- is named "external_call" - the name of the function it decorates


What does *failure* mean?
=========================
A *failure* is a raised exception, which was not caught during the function call.
By default, the circuit breaker listens for all exceptions based on the class ``Exception``.
That means, that all exceptions raised during the function call are considered as an
"expected failure" and will increase the failure count.

Get specific about the expected failure
=======================================
It is important, to be **as specific as possible**, when defining the expected exception.
The main purpose of a circuit breaker is to protect your distributed system from a cascading failure.
That means, you probably want to open the circuit breaker only, if the integration point on the other
end is unavailable. So e.g. if there is an ``ConnectionError`` or a request ``Timeout``.

If you are e.g. using the requests library (https://docs.python-requests.org/) for making HTTP calls,
its ``RequestException`` class would be a great choice for the ``expected_exception`` parameter.

The logic for treating thrown exceptions as failures can also be customized by passing a callable. The
callable will be passed the exception type and value, and should return True if the exception should be
treated as a failure.

All recognized exceptions will be re-raised anyway, but the goal is, to let the circuit breaker only
recognize those exceptions which are related to the communication to your integration point.

When it comes to monitoring (see Monitoring_), it may lead to falsy conclusions, if a
circuit breaker opened, due to a local ``OSError`` or ``KeyError``, etc.


Configuration
-------------
The following configuration options can be adjusted via decorator parameters. For example::

    from circuitbreaker import circuit

    @circuit(failure_threshold=10, expected_exception=ConnectionError)
    def external_call():
        ...



failure threshold
=================
By default, the circuit breaker opens after 5 subsequent failures. You can adjust this value with the ``failure_threshold`` parameter.

recovery timeout
================
By default, the circuit breaker stays open for 30 seconds to allow the integration point to recover.
You can adjust this value with the ``recovery_timeout`` parameter.

expected exception
==================
By default, the circuit breaker listens for all exceptions which are based on the ``Exception`` class.
You can adjust this with the ``expected_exception`` parameter. It can be either an exception class, an iterable of an exception classes,
or a callable.

Use a callable if the logic to flag exceptions as failures is more complex than a type check. For example::

    # Assume we are using the requests library
    def is_not_http_error(thrown_type, thrown_value):
        return issubclass(thrown_type, RequestException) and not issubclass(thrown_type, HTTPError)

    def is_rate_limited(thrown_type, thrown_value):
        return issubclass(thrown_type, HTTPError) and thrown_value.status_code == 429

    @circuit(expected_exception=is_not_http_error)
    def call_flaky_api(...):
        rsp = requests.get(...)
        rsp.raise_for_status()
        return rsp

    @circuit(expected_exception=is_rate_limited)
    def call_slow_server(...):
        rsp = requests.get(...)
        rsp.raise_for_status()
        return rsp
        ```

name
====
By default, the circuit breaker name is the name of the function it decorates. You can adjust the name with parameter ``name``.

fallback function
=================
By default, the circuit breaker will raise a ``CircuitBreaker`` exception when the circuit is opened.
You can instead specify a function to be called when the circuit is opened. This function can be specified with the
``fallback_function`` parameter and will be called with the same parameters as the decorated function would be.

Advanced Usage
--------------
If you apply circuit breakers to a couple of functions and you always set specific options other than the default values,
you can extend the ``CircuitBreaker`` class and create your own circuit breaker subclass instead::

    from circuitbreaker import CircuitBreaker

    class MyCircuitBreaker(CircuitBreaker):
        FAILURE_THRESHOLD = 10
        RECOVERY_TIMEOUT = 60
        EXPECTED_EXCEPTION = RequestException


Now you have two options to apply your circuit breaker to a function. As an Object directly::

    @MyCircuitBreaker()
    def external_call():
        ...

Please note, that the circuit breaker class has to be initialized, you have to use a class instance as decorator (``@MyCircuitBreaker()``), not the class itself (``@MyCircuitBreaker``).

Or via the decorator proxy::

    @circuit(cls=MyCircuitBreaker)
    def external_call():
        ...


.. _Monitoring:

Monitoring
----------
To keep track of the health of your application and the state of your circuit breakers, every circuit breaker registers itself at the ``CircuitBreakerMonitor``. You can receive all registered circuit breakers via ``CircuitBreakerMonitor.get_circuits()``.

To get an aggregated health status, you can ask the Monitor via ``CircuitBreakerMonitor.all_closed()``. Or you can retrieve the currently open circuits via ``CircuitBreakerMonitor.get_open()`` and the closed circuits via ``CircuitBreakerMonitor.get_closed()``.