同步/异步断路器的实现
lasier的Python项目详细描述
激光
同步/异步断路器的实现
根据Nygard在你的杰作书Release It!:
[...] circuit breakers protect overeager gadget hounds from burning their houses down. The principle is the same: detect excess usage, fail first, and open the circuit. More abstractly, the circuit breaker exists to allow one subsystem (an electrical circuit) to fail (excessive current draw, possibly from a short circuit) without destroying the entire system (the house). Furthermore, once the danger has passed, the circuit breaker can be reset to restore full function to the system.
要求
- Python>;=3.7
安装
使用pip
:
pip install lasier
使用
要使用lasier断路器,您需要一个rule
和一个cache
(电路状态存储)实例
规则
一个Rule
是定义电路在何处打开或关闭的机制。在
最大故障规则
基于最大故障数的断路规则
^{pr2}$参数
Argument | Definition |
---|---|
max_failures | Maximum number of errors |
failure_cache_key | Cache key where the number of errors is incremented |
百分比故障规则
基于故障百分比的断路规则
fromlasier.circuit_breaker.rulesimportPercentageFailuresRulerule=PercentageFailuresRule(max_failures_percentage=60,failure_cache_key='my_cb',min_accepted_requests=100,request_cache_key='my_cb_request')
参数
^{tb2}$断路器
您可以使用带有context_managerf.ex的激光断路器:
fromlasier.circuit_breaker.syncimportCircuitBreaker...defsome_protected_func():withCircuitBreaker(rule=rule,cache=cache,failure_exception=ValueError,catch_exceptions=(KeyError,TypeError)):# some process
或decorator,f.ex:
fromlasier.circuit_breaker.asyncioimportcircuit_breaker...@circuit_breaker(rule=rule,cache=cache,failure_exception=ValueError,catch_exceptions=(KeyError,TypeError))asyncdefsome_protected_func():# some process
sync和async实现遵循相同的接口,因此只需更改导入路径:
lasier.circuit_breaker.sync
:用于同步实现lasier.circuit_breaker.asyncio
:用于异步实现
参数
Argument | Definition |
---|---|
rule | Instance of class rule. |
cache | Instance of the circuit breaker state storage. |
failure_exception | Exception to be raised when it exceeds the maximum number of errors and when the circuit is open. |
failure_timeout | This value is set on first error. It is used to validate the number of errors by time. (seconds, default 60) |
circuit_timeout | Time that the circuit will be open. (seconds, default 60) |
catch_exceptions | List of exceptions catched to increase the number of errors. |
WARNING: The args
failure_timeout
andcircuit_timeout
will be used on state storage commands so if you'll use libs that expects milliseconds instead of seconds ontimeout
arguments maybe you'll get yourself in trouble
电路状态存储
Lasier与存储器一起工作,以记录电路的当前状态、故障次数等。该存储器遵循以下接口:
fromlasier.typesimportTimeout# Timeout = Optional[Union[int, float]]classStorage:defadd(self,key:str,value:int,timeout:Timeout=None)->None:passdefset(self,key:str,value:int,timeout:Timeout=None)->None:passdefincr(self,key:str)->int:passdefget(self,key:str)->int:passdefexpire(key:str,timeout:Timeout=None)->None:passdefdelete(self,key:str)->None:passdefflushdb(self)->None:pass
For
async
circuit breaker, lasier works with that same interface however with async syntax, f.ex:async def set(self, key=str, value=int, timeout=Optional[int])
因此,您可以使用任何符合该接口的缓存/存储。在
适配器
如果您将Lasier与redis-py一起用作缓存,那么您可以使用lasier.adapters.caches.redis.RedisAdapter
fromlasier.adapters.cachesimportRedisAdapterfromredisimportRediscache=RedisAdapter(Redis(host='localhost',port=6479,db=0))
实现的适配器
Lib | Adapter |
---|---|
redis-py | ^{ |
django-cache | ^{ |
aiocache | ^{ |
- 项目
标签: