6
6
7
7
class RateLimiter :
8
8
"""
9
- 线程安全的速率限制器,支持RPM(每分钟请求数)限制
9
+ Thread-safe rate limiter with RPM (requests per minute) support
10
10
"""
11
11
12
12
def __init__ (self , max_requests : int , window_seconds : int = 60 ):
13
13
"""
14
- 初始化速率限制器
14
+ Initialize rate limiter
15
15
16
16
Args:
17
- max_requests: 在指定时间窗口内允许的最大请求数
18
- window_seconds: 时间窗口大小(秒),默认60秒(1分钟)
17
+ max_requests: Maximum number of requests allowed within the specified time window
18
+ window_seconds: Time window size in seconds, default is 60 seconds (1 minute)
19
19
"""
20
20
self .max_requests = max_requests
21
21
self .window_seconds = window_seconds
@@ -24,74 +24,74 @@ def __init__(self, max_requests: int, window_seconds: int = 60):
24
24
25
25
def acquire (self , timeout : Optional [float ] = None ) -> bool :
26
26
"""
27
- 尝试获取请求许可
27
+ Try to acquire request permission
28
28
29
29
Args:
30
- timeout: 超时时间(秒),None表示无限等待
30
+ timeout: Timeout in seconds, None means infinite wait
31
31
32
32
Returns:
33
- bool: 是否成功获取许可
33
+ bool: Whether permission was successfully acquired
34
34
"""
35
35
start_time = time .time ()
36
36
37
37
while True :
38
38
with self ._lock :
39
39
current_time = time .time ()
40
40
41
- # 清理过期的请求记录
41
+ # Clean up expired request records
42
42
while self .requests and current_time - self .requests [0 ] > self .window_seconds :
43
43
self .requests .popleft ()
44
44
45
- # 检查是否可以发送请求
45
+ # Check if request can be sent
46
46
if len (self .requests ) < self .max_requests :
47
47
self .requests .append (current_time )
48
48
return True
49
49
50
- # 检查超时
50
+ # Check timeout
51
51
if timeout is not None and time .time () - start_time >= timeout :
52
52
return False
53
53
54
- # 等待一小段时间再重试
54
+ # Wait a short time before retrying
55
55
time .sleep (0.1 )
56
56
57
57
def wait_and_acquire (self ) -> None :
58
58
"""
59
- 等待直到可以获取请求许可(阻塞式)
59
+ Wait until request permission can be acquired (blocking)
60
60
"""
61
61
self .acquire (timeout = None )
62
62
63
63
def get_remaining_requests (self ) -> int :
64
64
"""
65
- 获取当前时间窗口内剩余的请求数
65
+ Get remaining requests in current time window
66
66
67
67
Returns:
68
- int: 剩余请求数
68
+ int: Number of remaining requests
69
69
"""
70
70
with self ._lock :
71
71
current_time = time .time ()
72
72
73
- # 清理过期的请求记录
73
+ # Clean up expired request records
74
74
while self .requests and current_time - self .requests [0 ] > self .window_seconds :
75
75
self .requests .popleft ()
76
76
77
77
return max (0 , self .max_requests - len (self .requests ))
78
78
79
79
def get_reset_time (self ) -> Optional [float ]:
80
80
"""
81
- 获取下次可以发送请求的时间戳
81
+ Get timestamp when next request can be sent
82
82
83
83
Returns:
84
- Optional[float]: 下次可发送请求的时间戳,None表示立即可发送
84
+ Optional[float]: Timestamp when next request can be sent, None means can send immediately
85
85
"""
86
86
with self ._lock :
87
87
current_time = time .time ()
88
88
89
- # 清理过期的请求记录
89
+ # Clean up expired request records
90
90
while self .requests and current_time - self .requests [0 ] > self .window_seconds :
91
91
self .requests .popleft ()
92
92
93
93
if len (self .requests ) < self .max_requests :
94
94
return None
95
95
96
- # 返回最早请求过期的时间
96
+ # Return the expiration time of the earliest request
97
97
return self .requests [0 ] + self .window_seconds
0 commit comments