同步/异步断路器的实现

lasier的Python项目详细描述


激光

同步/异步断路器的实现

Build Status

根据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}$
参数
ArgumentDefinition
max_failuresMaximum number of errors
failure_cache_keyCache 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

syncasync实现遵循相同的接口,因此只需更改导入路径:

  • lasier.circuit_breaker.sync:用于同步实现
  • lasier.circuit_breaker.asyncio:用于异步实现
参数
ArgumentDefinition
ruleInstance of class rule.
cacheInstance of the circuit breaker state storage.
failure_exceptionException to be raised when it exceeds the maximum number of errors and when the circuit is open.
failure_timeoutThis value is set on first error. It is used to validate the number of errors by time. (seconds, default 60)
circuit_timeoutTime that the circuit will be open. (seconds, default 60)
catch_exceptionsList of exceptions catched to increase the number of errors.

WARNING: The args failure_timeout and circuit_timeout will be used on state storage commands so if you'll use libs that expects milliseconds instead of seconds on timeout 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))

实现的适配器

LibAdapter
redis-py^{}
django-cache^{}
aiocache^{}

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java在SWT中关闭CTabItem时如何获取警告消息?   java如何从中获取文本字符串   java带有(int[][])的方法意味着什么?   java我在创建这个安卓浮动泡泡动画时做错了什么?   将边距属性作为列表项的java表抛出异常ClassCastException   java如何在Storm拓扑中测量延迟和吞吐量   java如何在javafx中序列化事件?   java访问main()之外的线程   java如何强制某些方法仅对kotlin可见   java如何使用quartzscheduler启动具有多个crontrigger的作业?   java无法使用VM选项获取转储文件:引发OOM异常时出现HEAPDUMPONAUTOFMEMORYERROR   java无法在安卓中的FTP服务器上上载文件   java RecordView未显示   java有没有办法在Eclipse中隐藏/折叠虚张声势的注释?   java如何从xml中提取xml。广州?