feat: Implement a short initial connection timeout for `_http_get_with_retry` and add `httpx.ReadTimeout` to the retry exceptions.
Build and Push CodeReview / build (push) Waiting to run Details

This commit is contained in:
vinland100 2026-02-09 10:53:08 +08:00
parent 5491714cce
commit a554503e04
1 changed files with 21 additions and 3 deletions

View File

@ -61,12 +61,30 @@ async def _request_with_retry(
headers: Dict[str, str], headers: Dict[str, str],
max_retries: int = 3, max_retries: int = 3,
) -> httpx.Response: ) -> httpx.Response:
"""带自动重试的 HTTP GET 请求,针对连接超时进行重试""" """带自动重试的 HTTP GET 请求,针对连接超时进行重试
首次尝试使用较短的超时时间2快速判断连接是否成功
后续重试使用默认超时时间避免长时间阻塞用户
"""
last_exc: Optional[Exception] = None last_exc: Optional[Exception] = None
# 首次尝试使用短超时2秒快速探测连接
first_attempt_timeout = httpx.Timeout(
connect=2.0, # 首次连接超时 2 秒
read=10.0, # 首次读取超时 10 秒
write=10.0,
pool=2.0,
)
for attempt in range(max_retries): for attempt in range(max_retries):
try: try:
return await client.get(url, headers=headers) if attempt == 0:
except (httpx.ConnectTimeout, httpx.ConnectError) as e: # 首次尝试:使用短超时快速探测
return await client.get(url, headers=headers, timeout=first_attempt_timeout)
else:
# 后续重试使用默认超时client 配置的 _GIT_API_TIMEOUT
return await client.get(url, headers=headers)
except (httpx.ConnectTimeout, httpx.ConnectError, httpx.ReadTimeout) as e:
last_exc = e last_exc = e
if attempt < max_retries - 1: if attempt < max_retries - 1:
wait = (attempt + 1) * 2 wait = (attempt + 1) * 2