超级天然蟒蛇限制器
python-throttle的Python项目详细描述
python redis支持的限制器
滑木或固定车窗限制器
此模块主要提供两个限制器
- 固定窗口限制器
简单地使用redis incr,它的速度大约是滑动版本的10倍,但限制并不平滑,可能会在两个间隔之间的间隙附近溢出阈值大小 - 滑动窗口限制器
使用redis有序集,速度慢,但提供更平滑的限制和更可扩展性
安装
pip install python-throttle
虚拟示例用法:
提醒:使用名称空间以避免同一个键上可能发生的冲突
importtimefromlimiterimportFixedWindowLimiterTEST_REDIS_CONFIG={'host':'localhost','port':6379,'db':10}ip="who are you?"throttle=FixedWindowLimiter(threshold=2,interval=3,redis_config=TEST_REDIS_CONFIG,name_space="default")print("first time, blocked?: {}".format(throttle.exceeded(ip)))print("second time, blocked?: {}".format(throttle.exceeded(ip)))print("now I block you, blocked?: {}".format(throttle.exceeded(ip)))time.sleep(3)print("refill energy, blocked?: {}".format(throttle.exceeded(ip)))
输出:
first time blocked?: False
second time blocked?: False
now I block you blocked?: True
refill energy blocked?: False
假海滩标志
它来自我的unittest,不准确,
rate_counter_pressure_test: SlidingRedisCounter
rate_counter_pressure_test: SlidingRedisCounter time count: 1.0075314449495636
rate_counter_pressure_test: FixedWindowRedisCounter
rate_counter_pressure_test: FixedWindowRedisCounter time count: 0.13711917598266155