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

@@ -180,3 +180,73 @@ async def test_prepare_usage_record_infers_ttl_from_1h_cache_split(
assert _DummyBillingService.last_dimensions is not None
assert _DummyBillingService.last_dimensions.get("cache_ttl_minutes") == 60
@pytest.mark.asyncio
async def test_prepare_usage_record_deserializes_body_json_before_build_params(
monkeypatch: pytest.MonkeyPatch,
) -> None:
db = MagicMock()
monkeypatch.setattr("src.services.billing.service.BillingService", _DummyBillingService)
monkeypatch.setattr(
"src.services.usage._billing_integration.sanitize_request_metadata",
lambda metadata: metadata,
)
captured: dict[str, Any] = {}
def _capture_build_usage_params(**kwargs: Any) -> dict[str, Any]:
captured.update(kwargs)
return {"total_cost_usd": 0.0, "actual_total_cost_usd": 0.0}
monkeypatch.setattr(
"src.services.usage._billing_integration.build_usage_params",
_capture_build_usage_params,
)
params = _build_params(db)
params.request_body = '{"messages":[{"role":"user","content":"hello"}]}'
params.provider_request_body = '{"tools":[{"name":"calc"}]}'
params.response_body = '{"choices":[{"index":0}]}'
params.client_response_body = '{"output":[{"type":"text"}]}'
await _TestUsageBillingIntegration._prepare_usage_record(params)
assert isinstance(captured["request_body"], dict)
assert captured["request_body"]["messages"][0]["content"] == "hello"
assert isinstance(captured["provider_request_body"], dict)
assert isinstance(captured["response_body"], dict)
assert isinstance(captured["client_response_body"], dict)
@pytest.mark.asyncio
async def test_prepare_usage_record_keeps_invalid_json_body_string(
monkeypatch: pytest.MonkeyPatch,
) -> None:
db = MagicMock()
monkeypatch.setattr("src.services.billing.service.BillingService", _DummyBillingService)
monkeypatch.setattr(
"src.services.usage._billing_integration.sanitize_request_metadata",
lambda metadata: metadata,
)
captured: dict[str, Any] = {}
def _capture_build_usage_params(**kwargs: Any) -> dict[str, Any]:
captured.update(kwargs)
return {"total_cost_usd": 0.0, "actual_total_cost_usd": 0.0}
monkeypatch.setattr(
"src.services.usage._billing_integration.build_usage_params",
_capture_build_usage_params,
)
invalid_json = '{"content":"x...[truncated]'
params = _build_params(db)
params.request_body = invalid_json
await _TestUsageBillingIntegration._prepare_usage_record(params)
assert captured["request_body"] == invalid_json