diff --git a/backend/app/services/scanner.py b/backend/app/services/scanner.py index c9f84b9..14c04d0 100644 --- a/backend/app/services/scanner.py +++ b/backend/app/services/scanner.py @@ -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]