rafa-acioly / circuitpy

Abstract circuit breaker implementation for python

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Circuitpy

Circuitpy is a abstract Python library for dealing with circuit breakers, it makes easy to use with anything or anywhere, you can use with redis, sql, in memory cache, apis and etc.

Installation

Use the package manager pip to install foobar.

pip install circuitpy

Usage

First you need to choose which storage you gonna use, you can use anything as storage since it implements three methods from Storage interface

Tip: Redis lib already has this methods by default, so it's easy to use.

Using redis

import redis
from circuitpy.base import BaseCircuitBreaker

redis_cli = redis.Redis(host='localhost', port=6379, db=0)

class RequestCircuitBreaker(BaseCircuitBreaker):

    storage = redis_cli
    expected_exception = (Exception,)
    failure_threshold = 100
    recovery_timeout = 1
    failure_key = "request_cb"

Using custom storage

from circuitpy.base import Storage

class CustomStorage(Storage):

    def increment(self, key: str) -> None:
        """increment the errors quantity"""

    def get(self, key: str) -> int:
        """retrieve the errors quantity"""

    def expire(self, key: str, ttl: int) -> None:
        """implement some sort of expiration for the key parameter"""
from circuitpy.base import BaseCircuitBreaker

class RequestCircuitBreaker(BaseCircuitBreaker):

    storage = CustomStorage()
    expected_exception = (Exception,)
    failure_threshold = 100
    recovery_timeout = 1
    failure_key = "request_cb"

Implementation

from circuitpy.circuit import circuit_breaker
from circuitpy.exceptions import CircuitBreakerOpen

@circuit_breaker(handler=RequestCircuitBreaker())
def lazy_method():
    try:
        # do stuff
    except CircuitBreakerOpen as open_circuit:
        logger.info(open_circuit)

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

About

Abstract circuit breaker implementation for python

License:MIT License


Languages

Language:Python 99.4%Language:Makefile 0.6%