fix: Adjust Git API timeouts and increase retry attempts to improve connection reliability.
Build and Push CodeReview / build (push) Has been cancelled Details

This commit is contained in:
vinland100 2026-02-28 10:11:10 +08:00
parent 3c5f67f49c
commit 7798422596
1 changed files with 12 additions and 12 deletions

View File

@ -30,10 +30,10 @@ logger = logging.getLogger(__name__)
# 解决"每天第一次打开时因 DNS/连接冷启动导致超时"的问题。
# ---------------------------------------------------------------------------
_GIT_API_TIMEOUT = httpx.Timeout(
connect=15.0, # 连接超时 15 秒(原来 120 秒太长,用户体验差)
read=60.0, # 读取超时 60 秒(大仓库 tree API 可能较慢)
write=30.0,
pool=15.0, # 从连接池获取连接的超时
connect=1.0, # 连接超时 15 秒(原来 120 秒太长,用户体验差)
read=10.0, # 读取超时 60 秒(大仓库 tree API 可能较慢)
write=60.0,
pool=2.0, # 从连接池获取连接的超时
)
_http_client: Optional[httpx.AsyncClient] = None
@ -59,7 +59,7 @@ async def _request_with_retry(
client: httpx.AsyncClient,
url: str,
headers: Dict[str, str],
max_retries: int = 3,
max_retries: int = 5,
) -> httpx.Response:
"""带自动重试的 HTTP GET 请求,针对连接超时进行重试
@ -70,10 +70,10 @@ async def _request_with_retry(
# 首次尝试使用短超时2秒快速探测连接
first_attempt_timeout = httpx.Timeout(
connect=2.0, # 首次连接超时 2
read=10.0, # 首次读取超时 10 秒
write=10.0,
pool=2.0,
connect=1.0, # 首次连接超时 1
read=10.0, # 超过 10 秒没有收到任何响应数据
write=60.0, # 超过 10 秒没有发送任何请求数据
pool=2.0, # 超过 2 秒 等待从连接池获取连接
)
for attempt in range(max_retries):
@ -87,11 +87,11 @@ async def _request_with_retry(
except (httpx.ConnectTimeout, httpx.ConnectError, httpx.ReadTimeout) as e:
last_exc = e
if attempt < max_retries - 1:
wait = (attempt + 1) * 2
logger.info(f"[API] 连接失败 (第 {attempt + 1} 次), {wait}s 后重试: {url} - {e}")
wait = (attempt + 1)
logger.info(f"[API] 连接失败 (第 {attempt + 1} 次), {wait}s 后重试: {url} - {type(e).__name__}: {e}")
await asyncio.sleep(wait)
else:
logger.error(f"[API] 连接最终失败 (共 {max_retries} 次): {url} - {e}")
logger.error(f"[API] 连接最终失败 (共 {max_retries} 次): {url} - {type(e).__name__}: {e}")
raise last_exc # type: ignore[misc]