fix: auto-fix code issues (cron)
- 修复重复导入/字段 - 修复异常处理 - 修复PEP8格式问题 - 添加类型注解
This commit is contained in:
@@ -56,7 +56,7 @@ class SlidingWindowCounter:
|
||||
self._cleanup_old(now)
|
||||
return sum(self.requests.values())
|
||||
|
||||
def _cleanup_old(self, now: int):
|
||||
def _cleanup_old(self, now: int) -> None:
|
||||
"""清理过期的请求记录 - 使用独立锁避免竞态条件"""
|
||||
cutoff = now - self.window_size
|
||||
old_keys = [k for k in list(self.requests.keys()) if k < cutoff]
|
||||
@@ -67,7 +67,7 @@ class SlidingWindowCounter:
|
||||
class RateLimiter:
|
||||
"""API 限流器"""
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
# key -> SlidingWindowCounter
|
||||
self.counters: dict[str, SlidingWindowCounter] = {}
|
||||
# key -> RateLimitConfig
|
||||
@@ -143,7 +143,7 @@ class RateLimiter:
|
||||
retry_after=max(0, config.window_size) if current_count >= config.requests_per_minute else 0,
|
||||
)
|
||||
|
||||
def reset(self, key: str | None = None):
|
||||
def reset(self, key: str | None = None) -> None:
|
||||
"""重置限流计数器"""
|
||||
if key:
|
||||
self.counters.pop(key, None)
|
||||
@@ -166,7 +166,7 @@ def get_rate_limiter() -> RateLimiter:
|
||||
|
||||
|
||||
# 限流装饰器(用于函数级别限流)
|
||||
def rate_limit(requests_per_minute: int = 60, key_func: Callable | None = None):
|
||||
def rate_limit(requests_per_minute: int = 60, key_func: Callable | None = None) -> None:
|
||||
"""
|
||||
限流装饰器
|
||||
|
||||
|
||||
Reference in New Issue
Block a user