refactor: 共享请求管道、按需懒加载、流式内存护栏与连接池治理

- 抽取 ApiRequestPipeline 单例,44 个路由文件共享同一实例
- Handler/Adapter 模块级 __getattr__ 延迟导入,减少启动时间
- 新增 ensure_stream_buffer_limit() 流式内存护栏(16MB 单行 / 32MB 总量)
- HTTP 空闲连接清理与 curl_cffi LRU 会话池
- ensure_providers_bootstrapped 按需引导指定 provider_types
- Usage 事件序列化迁移至 msgpack,Redis codec 隔离
- 启动预热任务(/readyz 就绪门控)与优雅关闭
- 通知邮件模块独立开关与 SMTP 配置校验
- CryptoService DCL 线程安全修复
- 通知模块开关 DB 查询 30s 内存缓存
- /readyz 对 unknown 状态返回 503
- 预热关闭 5s 超时保护
- 预热适配器逐个 try-except 容错
- FormatConversionRegistry 哨兵模式防并发重复物化
- 流式缓冲检查无条件执行

Closes #230

Co-authored-by: AAEE86 <ppk0227@hotmail.com>
This commit is contained in:
fawney19
2026-03-14 11:59:07 +08:00
parent 45985f1c04
commit e0286aebe3
111 changed files with 2775 additions and 1102 deletions

View File

@@ -0,0 +1,59 @@
from __future__ import annotations
import time
import pytest
from src.clients.http_client import HTTPClientPool
class _DummyClient:
def __init__(self, *, is_closed: bool = False) -> None:
self.is_closed = is_closed
self.close_calls = 0
async def aclose(self) -> None:
self.close_calls += 1
self.is_closed = True
@pytest.mark.asyncio
async def test_cleanup_idle_clients_closes_stale_entries(monkeypatch: pytest.MonkeyPatch) -> None:
now = time.time()
stale_proxy = _DummyClient()
active_proxy = _DummyClient()
already_closed_proxy = _DummyClient(is_closed=True)
stale_tunnel = _DummyClient()
monkeypatch.setattr(
HTTPClientPool,
"_proxy_clients",
{
"stale": (stale_proxy, now - 1200),
"active": (active_proxy, now - 10),
"closed": (already_closed_proxy, now - 1200),
},
raising=False,
)
monkeypatch.setattr(
HTTPClientPool,
"_tunnel_clients",
{"tunnel-stale": (stale_tunnel, now - 1200)},
raising=False,
)
stats = await HTTPClientPool.cleanup_idle_clients(max_idle_seconds=600)
assert stats["proxy_closed"] == 1
assert stats["tunnel_closed"] == 1
assert stats["proxy_already_closed"] == 1
assert stats["tunnel_already_closed"] == 0
assert stale_proxy.close_calls == 1
assert stale_tunnel.close_calls == 1
assert active_proxy.close_calls == 0
assert "active" in HTTPClientPool._proxy_clients
assert "stale" not in HTTPClientPool._proxy_clients
assert "closed" not in HTTPClientPool._proxy_clients
assert "tunnel-stale" not in HTTPClientPool._tunnel_clients