上述的漏斗限流算法,在Redis的模塊中已經內置實現了一個,具體介紹請參見Github redis-cell詳細介紹 筆者安裝在MacOS上,基本沒有問題:
# 下載mac版本安裝包
https://github.com/brandur/redis-cell/releases
# 解壓
tar -zxf redis-cell-*.tar.gz
# 復制可執行文件
cp libredis_cell.dylib /your_redis_server_localtion
# 重啟redis-server,把libredis_cell.dylib加載上
redis-server --loadmodule /path/to/modules/libredis_cell.dylib
1
2
3
4
5
6
7
8
安裝重啟后,可以在redis中執行 CL.THROTTLE 命令:
# CL.THROTTLE user123 15 30 60 1和實現算法中的配置類似,user123表示限流key,15: capacity,30: total,60: duration,
127.0.0.1:6379> CL.THROTTLE user123 15 30 60 1
1) (integer) 0 # 0表示允許,1表示拒絕
2) (integer) 16 # 漏斗容量 max_burst + 1 = 15 +1 =16
3) (integer) 15 # 漏斗剩余容量
4) (integer) -1 # 如果被拒絕,多少秒后重試
5) (integer) 2 # 多長時間后漏斗完全漏空
1
2
3
4
5
6
7
但是redis-cell沒有找到對應的sdk
Python Bound method
# python 3.x
def func():
pass
class A:
@classmethod
def method_cls(cls):
pass
def method_a(self):
pass
class B(A):
pass
a, b = A(), B()
print(func) # <function func at 0x10ee8a1e0>
print(a.method_a) # <bound method A.method_a of <__main__.A object at 0x10ef11978>>
print(b.method_cls) # <bound method A.method_cls of <class '__main__.B'>>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
對於上文中 func就是一個函數對象,而method_a 和 method_cls 是歸屬類A的所以,是一個bound method,那么如何查看一個 bound method的歸屬呢?
Python 2.x中提供了 im_func,im_class,im_self三個屬性:
im_func is the function object.
im_class is the class the method comes from.
im_self is the self object the method is bound to.
Python3.x中
__func__ replace im_func
__self__ replace im_self
2.x中的 im_class取消
# python 3.x
print(a.method_a.__self__)
print(b.method_cls.__self__)
# print(func.__self__) error func 無 __self__
print(b.method_cls.__self__.__name__)
# print(b.method_cls.__self__.__name__) error b.method_cls.__self__是一個實例,無__name__屬性
1
2
3
4
5
6
關於 __name__ 和 __qualname__ 請參見 PEP 3155
————————————————
版權聲明:本文為CSDN博主「Inevitable-Neo」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/duxiangwushirenfei/article/details/99982959