fix(backend): Kimi OAuth 请求级 httpx 客户端改生成器依赖修泄漏(CR-H2)

This commit is contained in:
Yaojia Wang
2026-07-08 10:40:14 +02:00
parent ca8fbc89a1
commit 5dd336b011
2 changed files with 61 additions and 3 deletions

View File

@@ -18,6 +18,7 @@ from __future__ import annotations
import asyncio
import uuid
from collections.abc import AsyncIterator
from typing import Annotated, Any
import httpx
@@ -72,8 +73,23 @@ _JOB_KIND_KIMI_OAUTH = "kimi_oauth"
_MAX_POLL_ATTEMPTS = 200
#: 请求/后台 HTTP 客户端超时(秒)。
_HTTP_TIMEOUT_SECONDS = 30.0
def _default_http_client() -> AsyncHttpClient:
return httpx.AsyncClient(timeout=30.0)
"""裸工厂:现建一个 httpx 客户端(**后台轮询**直接调用,自行在 finally 里 aclose"""
return httpx.AsyncClient(timeout=_HTTP_TIMEOUT_SECONDS)
async def _http_client() -> AsyncIterator[AsyncHttpClient]:
"""请求级 httpx 客户端**生成器依赖**:请求结束时经 `async with` 自动 acloseCR-H2
普通函数依赖无 teardown返回的 client 永不关闭 → 每次 `/start` 泄漏一个连接。故请求路径
用此生成器依赖(后台路径仍用裸工厂 `_default_http_client`,自管生命周期)。
"""
async with httpx.AsyncClient(timeout=_HTTP_TIMEOUT_SECONDS) as http:
yield http
# SlowDown 退避每次增大的间隔量(秒)。
@@ -163,7 +179,7 @@ async def start_oauth(
job_repo: JobRepoDep,
session: SessionDep,
session_factory: SessionFactoryDep,
http: Annotated[AsyncHttpClient, Depends(_default_http_client)],
http: Annotated[AsyncHttpClient, Depends(_http_client)],
) -> OAuthStartResponse:
"""发起 device authorization返回 202 + user_code/verification_uri后台轮询 token。