Skip to main content

Simple implementation of the Circuit Breaker pattern

Project description

A circuit breaker according to the logic outline in Michael T. Nygard's
great book [Release It!](http://www.amazon.com/Release-It-Production-Ready-Pragmatic-Programmers/dp/0978739213).

Read: http://en.wikipedia.org/wiki/Circuit_breaker_design_pattern

The circuit breaker monitors communication with a remote peer and in
the case of a high error rate may break the circuit and not allow
further communication for a short period. After a while the breaker
will let through a single request to probe to see if the peer feels
better. If it does, it will close the circuit and allow requests once
again. If not, it will open the circuit again.

A `CircuitBreakerSet` can handle the state for multiple peers at the
same time. Use the `context` method to pick which peer to track. The
first argument is used to identify the peer. Make it a string of some
kind, since it will be used to identify the peer in logs.

Below is a small example of how the circuit breaker can be used:

from circuit import CircuitBreakerSet
import logging, time

circuit_breaker = CircuitBreakerSet(time.time, logging.getLogger(
'circuit-breaker'))
circuit_breaker.handle_error(ValueError)

def fn(circuit_breaker):
try:
with circuit_breaker.context('my-remote-peer'):
raise ValueError('oh no')
except CircuitOpenError:
# the circuit was open so we did not even try to communicate
# with the remote service.
raise

If you call `fn` often enough the circuit breaker will open and
`CircuitOpenError` will be raised.

The `CircuitBreakerSet` class takes a few keyword arguments:

* `time_unit` (default 60) -- Number of seconds to sample seconds over.
* `maxfail` (default 3) -- Number of seconds that is allowed over a time unit.
* `reset_timeout` (default 10) -- Seconds that the circuit is open before
going into half-open mode.

It is also possible to create a single instance of a circuit breaker. The
`circuit.CircuitBreaker` class takes the following arguments:

* `clock` -- A callable that returns the time in seconds.
* `log` -- a `logging.Logger` object used for logging.
* `error_types` -- A list of error types that are treated as errors.
* `maxfail` -- Number of seconds that is allowed over a time unit.
* `reset_timeout` -- Seconds that the circuit is open before
going into half-open mode.
* `time_unit` -- Number of seconds to sample seconds over.


# Twisted Support #

There's also support for using the circuit breaker with Twisted. Note that
the circuit breaker still use pythons standard logging framework. Example:

from circuit import TwistedCircuitBreakerSet
import logger

circuit_breaker = TwistedCircuitBreakerSet(reactor, logging.getLogger(
'circuit-breaker'))

(The `TwistedCircuitBreakerSet` adds support for `defer.returnValue`
which uses exceptions internally.)

# Thanks #

* Michael Nygard, http://www.michaelnygard.com/, for writing the Release It!
book that outlines the circuit breaker pattern

* Edgeware, http://www.edgeware.tv/, for sponsoring the development of
python-circuit.

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

python-circuit-0.1.7.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

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

python_circuit-0.1.7-py2-none-any.whl (7.8 kB view details)

Uploaded Python 2

File details

Details for the file python-circuit-0.1.7.tar.gz.

File metadata

File hashes

Hashes for python-circuit-0.1.7.tar.gz
Algorithm Hash digest
SHA256 5ed783f6160a2204cdeef63fe6ae4bb56d1a0ae771acece44550140e5aacbdac
MD5 3e3a92b473d65bbf11ab5d52e183d295
BLAKE2b-256 5721b4001119ddd24b7cd77e8013152fc3d374a983a65c0983f98f5da8c204cf

See more details on using hashes here.

File details

Details for the file python_circuit-0.1.7-py2-none-any.whl.

File metadata

File hashes

Hashes for python_circuit-0.1.7-py2-none-any.whl
Algorithm Hash digest
SHA256 2d67b9829a5919f28928cdccc286bf875320ceb740973972993b6f1ae150943e
MD5 da76d37dd2d53c09d704cc6e440dc57f
BLAKE2b-256 f60675573abe076a5941e8345811cbb287db269aa95c689d787ffb566aab67ba

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