feat: 流式空闲超时、健康监控查询优化、限流桶内存上限与维护清理修复

Close #233

Co-authored-by: AAEE86 <ppk0227@hotmail.com>

- cli_monitor_mixin: 引入 STREAM_IDLE_TIMEOUT_SECONDS(可通过环境变量配置),
  流传输开始后若超出空闲窗口无新 chunk 则提前取消并返回 504,避免长时间挂起
- stream_context: 新增 managed_recorded_bodies 上下文管理器,确保 chunks 在
  telemetry 完成后及时释放;stream_telemetry 使用该接口统一管理 response body 构建
- health endpoint: 将状态聚合改为 GROUP BY 直接统计,事件列表按 api_format
  单独查询,避免单次 limit 拉取大量记录导致的遗漏与性能问题;同时过滤不活跃
  provider/endpoint,与公开健康接口保持一致
- endpoint health service: 修正时间线数据按 endpoint_id 而非 key_id 聚合
- token_bucket: 引入 max_buckets/bucket_expiry 上限与定时清理,防止内存无限增长;
  修复 refill_rate=0 时 get_reset_time 除零异常;新增 _is_unlimited_rate_limit 判断
- maintenance_scheduler: 调整清理顺序(先删整行再按窗口清理),新增 newer_than
  边界参数,避免同一行在同一轮中被重复改写
- sync_execute: 新增 create_pending_usage 开关,允许已预创建记录的调用方跳过重复创建
- quota_reader / provider_ops balance: 小幅修复与健壮性提升
- Dockerfile: 添加 MALLOC_ARENA_MAX=2 环境变量以降低 gunicorn worker RSS
- 补充相关测试覆盖
This commit is contained in:
fawney19
2026-03-18 23:38:26 +08:00
parent 3d5b6141a5
commit 1d72a8f9c1
37 changed files with 1787 additions and 607 deletions

View File

@@ -39,6 +39,17 @@ async def _yield_once_then_cancel(ctx: StreamContext) -> AsyncGenerator[bytes, N
raise asyncio.CancelledError()
async def _yield_once_then_hang(ctx: StreamContext) -> AsyncGenerator[bytes, None]:
ctx.append_text("partial output")
yield b"data: chunk\n\n"
await asyncio.sleep(3600)
async def _yield_after_delay_then_complete() -> AsyncGenerator[bytes, None]:
await asyncio.sleep(0.25)
yield b"data: first\n\n"
@pytest.mark.asyncio
async def test_create_monitored_stream_marks_client_disconnected_when_confirmed() -> None:
monitor = _DummyMonitor()
@@ -113,3 +124,38 @@ async def test_create_monitored_stream_estimates_output_tokens_before_unknown_ca
assert ctx.error_message == "cancelled_unknown"
assert ctx.output_tokens == expected_output_tokens
assert f"output_tokens={expected_output_tokens}" in (ctx.upstream_response or "")
@pytest.mark.asyncio
async def test_create_monitored_stream_marks_idle_timeout_before_worker_timeout() -> None:
monitor = _DummyMonitor()
monitor.CANCEL_DISCONNECT_RETRY_DELAYS_SECONDS = ()
monitor.STREAM_IDLE_TIMEOUT_SECONDS = 1.0
ctx = StreamContext(model="test-model", api_format="openai:cli", request_id="req-idle-timeout")
monitored = monitor._create_monitored_stream(ctx, _yield_once_then_hang(ctx), None)
with pytest.raises(asyncio.CancelledError):
async for _ in monitored:
pass
expected_output_tokens = max(1, len("partial output") // 4)
assert ctx.status_code == 504
assert ctx.error_message == "stream_idle_timeout"
assert ctx.output_tokens == expected_output_tokens
assert "cancel_origin=stream_idle_timeout" in (ctx.upstream_response or "")
@pytest.mark.asyncio
async def test_create_monitored_stream_does_not_idle_timeout_before_first_chunk() -> None:
monitor = _DummyMonitor()
monitor.CANCEL_DISCONNECT_RETRY_DELAYS_SECONDS = ()
monitor.STREAM_IDLE_TIMEOUT_SECONDS = 0.05
ctx = StreamContext(model="test-model", api_format="openai:cli", request_id="req-first-chunk")
monitored = monitor._create_monitored_stream(ctx, _yield_after_delay_then_complete(), None)
chunks = [chunk async for chunk in monitored]
assert chunks == [b"data: first\n\n"]
assert ctx.status_code == 200
assert ctx.error_message is None