调整相关代码

This commit is contained in:
Sakura-RanChen 2026-10-13 10:40:47 +08:00
parent e5f326285e
commit b8f23ffa66
3 changed files with 80 additions and 52 deletions

View file

@ -1,5 +1,4 @@
import os import os
import ssl
import base64 import base64
import asyncio import asyncio
import functools import functools
@ -9,13 +8,11 @@ from typing import Optional, Dict
import httpx import httpx
# 跨请求共享的 SSLContext:httpx 默认每个 AsyncClient 都重新
# create_default_context() 并加载系统 CA(100+ 证书,毫秒级 CPU)。
# Context 构造后只读,跨 client / 线程共享安全。
_SHARED_SSL_CONTEXT = ssl.create_default_context()
TAG = __name__ TAG = __name__
# _retry_wait 的分片粒度:safe_close 后 ~100ms 内收尾,轮询开销可忽略
_RETRY_WAIT_SLICE = 0.1
class DeviceNotFoundException(Exception): class DeviceNotFoundException(Exception):
pass pass
@ -65,13 +62,10 @@ class ManageApiClient:
def _build_client(cls) -> httpx.AsyncClient: def _build_client(cls) -> httpx.AsyncClient:
"""构造单次业务请求专用的 AsyncClient。 """构造单次业务请求专用的 AsyncClient。
client 生命周期与"一次业务请求(含重试)"对齐:调用方用 约束:client 生命周期与单次业务请求(含重试)对齐,由调用方
``async with`` 在创建它的同一事件循环里关闭它。不存在任何 ``async with`` 在同一事件循环内关闭,禁止跨请求 / 跨 loop
跨请求 / 跨 loop 缓存,临时 loop 守护线程场景同样安全, 缓存。锁内只做关闭检查与配置快照,构造在锁外执行,避免并发
不会出现客户端随 loop 累积的问题。 请求在 threading.Lock 上串行阻塞事件循环。
锁内只做关闭检查和配置快照,client 构造在锁外进行,
避免并发请求在 ``threading.Lock`` 上串行阻塞事件循环线程。
""" """
with cls._instance_lock: with cls._instance_lock:
if cls._closed: if cls._closed:
@ -87,18 +81,18 @@ class ManageApiClient:
"Authorization": "Bearer " + secret, "Authorization": "Bearer " + secret,
}, },
timeout=timeout, timeout=timeout,
# 连接复用限于单次业务请求内部:重试(含 retry_delay 级 # 约束:连接复用仅限单次业务请求内部,请求结束必关 client;
# sleep)期间 keep-alive 连接保持在池中,下一次尝试直接 # keepalive_expiry 动态覆盖 retry_delay,保证重试等待期间
# 复用,仅在连接真坏掉时才重新握手。keepalive_expiry 需 # 池内连接不过期;连接真坏掉由 RemoteProtocolError 重试兜底。
# 覆盖 retry_delay,否则 sleep 期间连接已被池判定过期,
# 复用落空(退化为每次尝试重新握手,行为仍正确)。
# 请求结束 async with 必关 client,连接不会跨请求驻留。
limits=httpx.Limits( limits=httpx.Limits(
max_keepalive_connections=1, max_keepalive_connections=1,
keepalive_expiry=30, keepalive_expiry=max(30, cls.retry_delay + 5),
), ),
trust_env=False, trust_env=False,
verify=_SHARED_SSL_CONTEXT, # 约束:不共享 SSLContext——httpcore 建连会原地改写传入的
# context(OpenSSL 侧无锁),共享单例并发握手有竞态;走
# httpx 默认路径按 client 现建(certifi 信任库)。
verify=True,
) )
@classmethod @classmethod
@ -118,14 +112,13 @@ class ManageApiClient:
raise DeviceBindException(result.get("msg")) raise DeviceBindException(result.get("msg"))
elif result.get("code") != 0: elif result.get("code") != 0:
raise Exception(f"API返回错误: {result.get('msg', '未知错误')}") raise Exception(f"API返回错误: {result.get('msg', '未知错误')}")
return result.get("data") if result.get("code") == 0 else None return result.get("data")
finally: finally:
if response is not None: if response is not None:
try: try:
await response.aclose() await response.aclose()
except Exception: except Exception:
# 吞掉释放异常:否则它会覆盖原始业务异常, # 约束:必须吞掉释放异常,避免覆盖业务异常、破坏重试判断
# 破坏 _execute_async_request 的重试判断
pass pass
@classmethod @classmethod
@ -137,11 +130,13 @@ class ManageApiClient:
): ):
return True return True
# 请求间启用了池内 keep-alive 复用:服务端 / 负载均衡可能在 # 约束:RemoteProtocolError 仅在"未收到响应"(without sending
# keepalive 窗口内提前回收连接,复用时表现为对端断开, # a response)时重试——响应从未开始,重试安全;响应截断
# 重新握手重试即可。 # (incomplete message body)变体意味着服务端可能已处理请求,
# 重试非幂等 POST 会重复提交。文案依赖 httpcore,变更时退化
# 为不重试(安全方向)。
if isinstance(exception, httpx.RemoteProtocolError): if isinstance(exception, httpx.RemoteProtocolError):
return True return "without sending a response" in str(exception)
# HTTP状态码错误 # HTTP状态码错误
if isinstance(exception, httpx.HTTPStatusError): if isinstance(exception, httpx.HTTPStatusError):
@ -152,16 +147,14 @@ class ManageApiClient:
@classmethod @classmethod
async def _retry_wait(cls): async def _retry_wait(cls):
"""可被 safe_close() 打断的重试等待。 """可被 safe_close() 打断的重试等待:100ms 分片轮询 _closed。
以 100ms 分片轮询 _closed:服务关闭后立即返回,由调用方 约束:不得一次睡满 retry_delay——否则服务关闭后在途请求要
跳过剩余重试。不分片一次睡满 retry_delay 的话,container 拖满 max_retries × retry_delay 才收尾,超出进程退出宽限期。
SIGTERM 场景下在途请求要拖满 max_retries × retry_delay
(默认 60s)才收尾,会吃光进程退出宽限期。
""" """
remaining = cls.retry_delay remaining = cls.retry_delay
while remaining > 0 and not cls._closed: while remaining > 0 and not cls._closed:
step = min(0.1, remaining) step = min(_RETRY_WAIT_SLICE, remaining)
await asyncio.sleep(step) await asyncio.sleep(step)
remaining -= step remaining -= step
@ -169,13 +162,10 @@ class ManageApiClient:
async def _execute_async_request(cls, method: str, endpoint: str, **kwargs) -> Dict: async def _execute_async_request(cls, method: str, endpoint: str, **kwargs) -> Dict:
"""带重试机制的业务请求执行器。 """带重试机制的业务请求执行器。
client 只构造一次,整个重试过程复用(重试间隔期间连接 约束:client 整个重试过程只构造一次,重试结束(成功或放弃)
keep-alive 在池内保持),重试结束(成功或放弃)后由 后由 ``async with`` 关闭并释放连接。safe_close() 不取消已开始
``async with`` 关闭 client 并释放连接。 的尝试,但会打断重试等待:不再发起新尝试,以最后一次失败
原因收尾。
safe_close() 不取消已开始的尝试(由它跑完当前这一次),
但会打断重试等待:之后不再发起新尝试,请求以最后一次
失败原因收尾。
""" """
client = cls._build_client() client = cls._build_client()
async with client: async with client:
@ -204,12 +194,11 @@ class ManageApiClient:
@classmethod @classmethod
def _get_instance(cls) -> Optional["ManageApiClient"]: def _get_instance(cls) -> Optional["ManageApiClient"]:
"""线程安全地获取单例实例引用 """线程安全地获取单例实例引用。
调用方应使用返回的局部引用,而不是判空后再次读取 约束:调用方必须持有并使用返回的局部引用,禁止判空后再次
ManageApiClient._instance:即使随后 safe_close() 将 _instance 读取 ManageApiClient._instance——safe_close() 可能在判空之后
置为 None,已获取的引用仍指向原对象,从而消除"判空之后、 将其置 None(TOCTOU 竞态窗口)。
使用之前实例被置空"的 TOCTOU 竞态窗口。
""" """
with cls._instance_lock: with cls._instance_lock:
return cls._instance return cls._instance
@ -218,7 +207,7 @@ class ManageApiClient:
def safe_close(cls): def safe_close(cls):
"""关闭请求入口;在途请求在各自事件循环中释放客户端。 """关闭请求入口;在途请求在各自事件循环中释放客户端。
已开始的请求不会被取消,但重试等待会被打断(加速收尾), 已开始的请求不取消;重试等待被打断、跳过剩余重试,
详见 _execute_async_request / _retry_wait。 详见 _execute_async_request / _retry_wait。
""" """
with cls._instance_lock: with cls._instance_lock:
@ -250,9 +239,12 @@ def api_guard(error_msg: str = None, raise_when_closed: bool = False):
try: try:
return await func(instance, *args, **kwargs) return await func(instance, *args, **kwargs)
except Exception as e: except Exception as e:
# format_exc 拼进同一条 print:保持模块统一走 stdout, # 约束:httpx / 解析异常打全堆栈;业务语义异常只打
# 且保留完整堆栈便于生产定位 # message(消息自带上下文,堆栈是高频噪声)
print(f"{error_msg}: {e}\n{traceback.format_exc()}") if isinstance(e, (httpx.HTTPError, ValueError)):
print(f"{error_msg}: {e}\n{traceback.format_exc()}")
else:
print(f"{error_msg}: {e}")
return None return None
return wrapper return wrapper

View file

@ -1,9 +1,14 @@
"""Offline reproduction: no real devices, credentials, HTTP server or model APIs. """Offline reproduction: no real devices, credentials, HTTP server or model APIs.
Usage: python -B reproduce.py path/to/config/manage_api_client.py Usage: python -B tests/reproduce_manage_api_client_retention.py path/to/config/manage_api_client.py
Keeps CLOSED loop objects alive to avoid Python reusing their integer IDs. Keeps CLOSED loop objects alive to avoid Python reusing their integer IDs.
This makes the cache-lifetime check deterministic; it is not an RSS benchmark. This makes the cache-lifetime check deterministic; it is not an RSS benchmark.
依赖真实 httpx.AsyncClient + MockTransport,不进 pytest 套件
(tests/conftest.py 的 mock 层会全局替换 AsyncClient)。
""" """
__test__ = False
import asyncio import asyncio
import gc import gc
import importlib.util import importlib.util

View file

@ -1,4 +1,12 @@
"""Run directly with unittest; no application/model dependencies or network.""" """ManageApiClient 生命周期回归测试:直接以 unittest 运行,不依赖应用/模型与网络。
不进 pytest 套件:tests/conftest.py 的 mock 层(import 时全局替换
asyncio.sleep 与 httpx.AsyncClient)与本文件的时序断言、MockTransport
注入互相破坏;__test__ = False 使 pytest 跳过收集,请直接用 unittest 运行。
Usage: python -B tests/test_manage_api_client_lifecycle.py -v
"""
__test__ = False
import asyncio import asyncio
from concurrent.futures import ThreadPoolExecutor from concurrent.futures import ThreadPoolExecutor
import importlib.util import importlib.util
@ -76,6 +84,29 @@ class ManageApiClientLifecycleTest(unittest.IsolatedAsyncioTestCase):
self.assertEqual(len(self.clients), 1) self.assertEqual(len(self.clients), 1)
self.assertTrue(self.clients[0].is_closed) self.assertTrue(self.clients[0].is_closed)
async def test_remote_protocol_error_without_response_triggers_retry(self):
"""stale keep-alive 场景:响应从未开始,重试安全。"""
async def handle(request):
if len(self.requests) == 1:
raise httpx.RemoteProtocolError(
'Server disconnected without sending a response.',
request=request)
return httpx.Response(200, json={'code': 0, 'data': True})
with self.transport(handle):
self.assertTrue(await self.cls._execute_async_request('POST', '/report'))
self.assertEqual(len(self.requests), 2)
async def test_remote_protocol_error_incomplete_body_not_retried(self):
"""响应中途截断:服务端很可能已处理完请求,重试非幂等 POST
会重复提交,必须直接失败。"""
async def handle(request):
raise httpx.RemoteProtocolError(
'Peer closed connection without sending complete message body '
'(discarded 10 of 100 read bytes)', request=request)
with self.transport(handle), self.assertRaises(httpx.RemoteProtocolError):
await self.cls._execute_async_request('POST', '/report')
self.assertEqual(len(self.requests), 1)
async def test_nonretryable_http_error(self): async def test_nonretryable_http_error(self):
async def handle(request): async def handle(request):
return httpx.Response(403) return httpx.Response(403)