Skip to main content
Donate to the Python Software Foundation or Purchase a PyCharm License to Benefit the PSF! Donate Now

Usable Circuit Breaker pattern implementation

Project description

https://travis-ci.org/elemepi/breakers.svg?branch=master

Usable Circuit Breaker pattern implementation.

Install

$ pip install breakers

Usage

import functools
from breakers import Breaker

def circuit_breaker(time_span=20000, unit=1000, calls_limit=10,
                    error_limit=0.5, retry_time=10000):
    def deco(func):
        # Create breaker
        if not hasattr(func, '__breaker__'):
            func.__breaker__ = Breaker(time_span, unit, calls_limit,
                                       error_limit, retry_time)
        breaker = func.__breaker__

        @functools.wraps(func)
        def wraps(*args, **kwargs):
            if not breaker.is_allow():
                raise RuntimeError('Circuit breaker')

            exc = None
            try:
                return func(*args, **kwargs)
            except Exception as e:
                exc = e
                raise
            finally:
                if exc:
                    breaker.add_failure(1)
                else:
                    breaker.add_success(1)
        return wraps
    return deco

@circuit_breaker()
def f():
    import random
    if random.randint(1, 4) in (1, 2):
        raise ValueError
    return 'succeed'

Release history Release notifications

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
breakers-0.1.0-py2.py3-none-any.whl (5.4 kB) Copy SHA256 hash SHA256 Wheel py2.py3
breakers-0.1.0.tar.gz (3.3 kB) Copy SHA256 hash SHA256 Source None

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page