fix: auto-fix code issues (cron)
- 修复重复导入/字段 - 修复异常处理 (将裸 except Exception 改为具体异常类型) - 修复PEP8格式问题 - 清理未使用导入 - 添加 UUID_LENGTH 常量替代魔法数字 - 添加 DEFAULT_RATE_LIMIT, MASTER_KEY_RATE_LIMIT, IP_RATE_LIMIT 常量 - 添加 MAX_TEXT_LENGTH, DEFAULT_TIMEOUT 常量 涉及文件: - backend/main.py - backend/db_manager.py - backend/llm_client.py - backend/neo4j_manager.py - backend/tingwu_client.py - backend/tenant_manager.py - backend/growth_manager.py - backend/workflow_manager.py - backend/image_processor.py - backend/multimodal_entity_linker.py - backend/multimodal_processor.py - backend/plugin_manager.py - backend/rate_limiter.py
This commit is contained in:
@@ -12,6 +12,7 @@ from collections.abc import Callable
|
||||
from dataclasses import dataclass
|
||||
from functools import wraps
|
||||
|
||||
|
||||
@dataclass
|
||||
class RateLimitConfig:
|
||||
"""限流配置"""
|
||||
@@ -20,6 +21,7 @@ class RateLimitConfig:
|
||||
burst_size: int = 10 # 突发请求数
|
||||
window_size: int = 60 # 窗口大小(秒)
|
||||
|
||||
|
||||
@dataclass
|
||||
class RateLimitInfo:
|
||||
"""限流信息"""
|
||||
@@ -29,6 +31,7 @@ class RateLimitInfo:
|
||||
reset_time: int # 重置时间戳
|
||||
retry_after: int # 需要等待的秒数
|
||||
|
||||
|
||||
class SlidingWindowCounter:
|
||||
"""滑动窗口计数器"""
|
||||
|
||||
@@ -60,6 +63,7 @@ class SlidingWindowCounter:
|
||||
for k in old_keys:
|
||||
self.requests.pop(k, None)
|
||||
|
||||
|
||||
class RateLimiter:
|
||||
"""API 限流器"""
|
||||
|
||||
@@ -155,9 +159,11 @@ class RateLimiter:
|
||||
self.counters.clear()
|
||||
self.configs.clear()
|
||||
|
||||
|
||||
# 全局限流器实例
|
||||
_rate_limiter: RateLimiter | None = None
|
||||
|
||||
|
||||
def get_rate_limiter() -> RateLimiter:
|
||||
"""获取限流器实例"""
|
||||
global _rate_limiter
|
||||
@@ -166,6 +172,8 @@ def get_rate_limiter() -> RateLimiter:
|
||||
return _rate_limiter
|
||||
|
||||
# 限流装饰器(用于函数级别限流)
|
||||
|
||||
|
||||
def rate_limit(requests_per_minute: int = 60, key_func: Callable | None = None) -> None:
|
||||
"""
|
||||
限流装饰器
|
||||
@@ -208,5 +216,6 @@ def rate_limit(requests_per_minute: int = 60, key_func: Callable | None = None)
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
class RateLimitExceeded(Exception):
|
||||
"""限流异常"""
|
||||
|
||||
Reference in New Issue
Block a user