harden(observe): 防重复 start 泄漏 client + 关停丢余量注释 + 补 stop/失败存活测试
评审建议(Task4 code-quality): - start 已启动则不重复建 client(避免泄漏旧连接池) - 关停只发一批、超出丢弃,补注释说明 best-effort - 补测: _run_loop 失败后存活 / stop drain+发最后一批+关 client Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -74,10 +74,14 @@ async def _run_loop(client: httpx.AsyncClient) -> None:
|
||||
|
||||
|
||||
def start_observe_worker() -> asyncio.Task | None:
|
||||
"""启动上报 worker。未配置观测 → 返回 None(no-op)。"""
|
||||
"""启动上报 worker。未配置观测 → 返回 None(no-op)。约定每进程只调一次(lifespan)。"""
|
||||
global _client
|
||||
if not settings.observe_configured:
|
||||
return None
|
||||
if _client is not None:
|
||||
# 约定 start 每进程只调一次;已启动则不重复建 client(避免泄漏旧连接池)。
|
||||
logger.warning("observe worker already started; ignoring duplicate start")
|
||||
return None
|
||||
_client = httpx.AsyncClient(
|
||||
base_url=settings.OBSERVE_ENDPOINT,
|
||||
auth=(settings.OBSERVE_USER, settings.OBSERVE_PASSWORD),
|
||||
@@ -101,7 +105,8 @@ async def stop_observe_worker(task: asyncio.Task | None) -> None:
|
||||
with contextlib.suppress(asyncio.CancelledError):
|
||||
await task
|
||||
if _client is not None:
|
||||
# worker 已停,安全 drain 剩余并 best-effort 发最后一批(短超时,不卡关停)。
|
||||
# worker 已停,安全 drain 剩余并 best-effort 发最后一批(短超时,不卡关停);
|
||||
# 超过一批(BATCH_MAX)的剩余直接丢,不做多轮 flush(best-effort,关停从速)。
|
||||
try:
|
||||
queue = get_queue()
|
||||
final: list[dict] = []
|
||||
|
||||
Reference in New Issue
Block a user