feat(u115): improve stability of the u115 module

1. 优化API请求错误时到处理逻辑
2. 提升hash计算速度
3. 接口级QPS速率限制
4. 使用httpx替换request
5. 优化路径拼接稳定性
6. 代码格式化
This commit is contained in:
DDSRem
2025-11-19 19:39:02 +08:00
parent edd44a0993
commit f589fcc2d0
2 changed files with 222 additions and 188 deletions

View File

@@ -382,3 +382,27 @@ def rate_limit_window(max_calls: int, window_seconds: float,
limiter = WindowRateLimiter(max_calls, window_seconds, source, enable_logging)
# 使用通用装饰器逻辑包装该限流器
return rate_limit_handler(limiter, raise_on_limit)
class QpsRateLimiter:
"""
速率控制器,精确控制 QPS
"""
def __init__(self, qps: float | int):
if qps <= 0:
qps = float("inf")
self.interval = 1.0 / qps
self.lock = threading.Lock()
self.next_call_time = time.monotonic()
def acquire(self) -> None:
"""
获取调用许可,阻塞直到满足速率限制
"""
with self.lock:
now = time.monotonic()
wait_time = self.next_call_time - now
if wait_time > 0:
time.sleep(wait_time)
self.next_call_time = max(now, self.next_call_time) + self.interval