mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 09:20:22 +08:00
refactor: 移除 Provider/Endpoint 级别的 timeout 配置,改用全局环境变量
- 废弃 Provider.timeout 和 ProviderEndpoint.timeout 字段(保留数据库列以兼容) - 非流式请求超时由 HTTP_REQUEST_TIMEOUT 环境变量控制(默认 300 秒) - 流式请求首字节超时由 STREAM_FIRST_BYTE_TIMEOUT 环境变量控制(默认 30 秒) - 移除前端表单中的超时配置输入框 - 简化 settings.py 中的 TTFB 超时解析逻辑 - 更新 API 请求/响应模型,移除 timeout 字段 - 更新导入导出功能,不再包含 timeout 配置
This commit is contained in:
@@ -214,16 +214,14 @@ class TimeoutDefaults:
|
||||
"""超时配置默认值(秒)
|
||||
|
||||
超时配置说明:
|
||||
- 全局默认值和 Provider 默认值统一为 120 秒
|
||||
- 120 秒是 LLM API 的合理默认值:
|
||||
* 大多数请求在 30 秒内完成
|
||||
* 复杂推理(如 Claude extended thinking)可能需要 60-90 秒
|
||||
* 120 秒足够覆盖大部分场景,同时避免线程池被长时间占用
|
||||
- 如需更长超时,可在 Provider 级别单独配置
|
||||
- 非流式请求超时由环境变量 HTTP_REQUEST_TIMEOUT 控制(默认 300 秒)
|
||||
- 流式请求首字节超时由环境变量 STREAM_FIRST_BYTE_TIMEOUT 控制(默认 30 秒)
|
||||
- 此处的常量仅用于无法访问 config 的场景(如模型查询测试)
|
||||
"""
|
||||
|
||||
# HTTP 请求默认超时(与 Provider 默认值保持一致)
|
||||
HTTP_REQUEST = 120 # 2分钟
|
||||
# HTTP 请求默认超时(用于模型查询等测试场景)
|
||||
# 与 config.http_request_timeout 默认值保持一致
|
||||
HTTP_REQUEST = 300 # 5分钟
|
||||
|
||||
# 数据库连接池获取超时
|
||||
DB_POOL = 30
|
||||
|
||||
@@ -144,6 +144,8 @@ class Config:
|
||||
self.http_read_timeout = float(os.getenv("HTTP_READ_TIMEOUT", "60.0"))
|
||||
self.http_write_timeout = float(os.getenv("HTTP_WRITE_TIMEOUT", "60.0"))
|
||||
self.http_pool_timeout = float(os.getenv("HTTP_POOL_TIMEOUT", "10.0"))
|
||||
# HTTP_REQUEST_TIMEOUT: 非流式请求整体超时(秒),默认 300 秒
|
||||
self.http_request_timeout = float(os.getenv("HTTP_REQUEST_TIMEOUT", "300.0"))
|
||||
|
||||
# HTTP 连接池配置
|
||||
# HTTP_MAX_CONNECTIONS: 最大连接数,影响并发能力
|
||||
@@ -167,10 +169,9 @@ class Config:
|
||||
# STREAM_PREFETCH_LINES: 预读行数,用于检测嵌套错误
|
||||
# STREAM_STATS_DELAY: 统计记录延迟(秒),等待流完全关闭
|
||||
# STREAM_FIRST_BYTE_TIMEOUT: 首字节超时(秒),等待首字节超过此时间触发故障转移
|
||||
# 范围: 10-120 秒,默认 30 秒(必须小于 http_write_timeout 避免竞态)
|
||||
self.stream_prefetch_lines = int(os.getenv("STREAM_PREFETCH_LINES", "5"))
|
||||
self.stream_stats_delay = float(os.getenv("STREAM_STATS_DELAY", "0.1"))
|
||||
self.stream_first_byte_timeout = self._parse_ttfb_timeout()
|
||||
self.stream_first_byte_timeout = float(os.getenv("STREAM_FIRST_BYTE_TIMEOUT", "30.0"))
|
||||
|
||||
# 请求体读取超时(秒)
|
||||
# REQUEST_BODY_TIMEOUT: 等待客户端发送完整请求体的超时时间
|
||||
@@ -289,39 +290,6 @@ class Config:
|
||||
# 最小 10 个保活连接,最大不超过 max_connections
|
||||
return max(10, min(keepalive, self.http_max_connections))
|
||||
|
||||
def _parse_ttfb_timeout(self) -> float:
|
||||
"""
|
||||
解析 TTFB 超时配置,带错误处理和范围限制
|
||||
|
||||
TTFB (Time To First Byte) 用于检测慢响应的 Provider,超时触发故障转移。
|
||||
此值必须小于 http_write_timeout,避免竞态条件。
|
||||
|
||||
Returns:
|
||||
超时时间(秒),范围 10-120,默认 30
|
||||
"""
|
||||
default_timeout = 30.0
|
||||
min_timeout = 10.0
|
||||
max_timeout = 120.0 # 必须小于 http_write_timeout (默认 60s) 的 2 倍
|
||||
|
||||
raw_value = os.getenv("STREAM_FIRST_BYTE_TIMEOUT", str(default_timeout))
|
||||
try:
|
||||
timeout = float(raw_value)
|
||||
except ValueError:
|
||||
# 延迟导入,避免循环依赖(Config 初始化时 logger 可能未就绪)
|
||||
self._ttfb_config_warning = (
|
||||
f"无效的 STREAM_FIRST_BYTE_TIMEOUT 配置 '{raw_value}',使用默认值 {default_timeout}秒"
|
||||
)
|
||||
return default_timeout
|
||||
|
||||
# 范围限制
|
||||
clamped = max(min_timeout, min(max_timeout, timeout))
|
||||
if clamped != timeout:
|
||||
self._ttfb_config_warning = (
|
||||
f"STREAM_FIRST_BYTE_TIMEOUT={timeout}秒超出范围 [{min_timeout}-{max_timeout}],"
|
||||
f"已调整为 {clamped}秒"
|
||||
)
|
||||
return clamped
|
||||
|
||||
def _validate_pool_config(self) -> None:
|
||||
"""验证连接池配置是否安全"""
|
||||
total_per_worker = self.db_pool_size + self.db_max_overflow
|
||||
@@ -369,10 +337,6 @@ class Config:
|
||||
if hasattr(self, "_pool_config_warning") and self._pool_config_warning:
|
||||
logger.warning(self._pool_config_warning)
|
||||
|
||||
# TTFB 超时配置警告
|
||||
if hasattr(self, "_ttfb_config_warning") and self._ttfb_config_warning:
|
||||
logger.warning(self._ttfb_config_warning)
|
||||
|
||||
# 管理员密码检查(必须在环境变量中设置)
|
||||
if hasattr(self, "_missing_admin_password") and self._missing_admin_password:
|
||||
logger.error("必须设置 ADMIN_PASSWORD 环境变量!")
|
||||
|
||||
Reference in New Issue
Block a user