fix: 启动后60秒内所有请求使用15秒超时

问题:之前的 _first_request 只对第一个HTTP请求有效,但login()
需要两次请求(GET登录页+POST登录),导致实际的POST登录
请求仍然只有5秒超时,在冷启动时容易失败。

修复:改为基于模块启动时间的超时策略
- 启动后60秒内:所有请求使用15秒超时
- 60秒后:恢复正常的5秒超时

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2025-12-24 01:42:03 +08:00
parent 151fc3e09f
commit ec84903745

View File

@@ -18,6 +18,11 @@ from urllib.parse import urlsplit
from app_config import get_config
import time as _time_module
_MODULE_START_TIME = _time_module.time()
_WARMUP_PERIOD_SECONDS = 60 # 启动后 60 秒内使用更长超时
_WARMUP_TIMEOUT_SECONDS = 15.0 # 预热期间的超时时间
config = get_config()
BASE_URL = getattr(config, "ZSGL_BASE_URL", "https://postoa.aidunsoft.com")
@@ -71,7 +76,6 @@ class APIBrowser:
self.log_callback = log_callback
self.stop_flag = False
self._closed = False # 防止重复关闭
self._first_request = True
self.last_total_records = 0
# 设置代理
@@ -133,10 +137,9 @@ class APIBrowser:
def _request_with_retry(self, method, url, max_retries=3, retry_delay=1, **kwargs):
"""带重试机制的请求方法"""
# 首次请求使用更长超时10秒),后使用配置的超时
if self._first_request:
kwargs.setdefault('timeout', 10.0)
self._first_request = False
# 启动后 60 秒内使用更长超时15秒),后使用配置的超时
if (_time_module.time() - _MODULE_START_TIME) < _WARMUP_PERIOD_SECONDS:
kwargs.setdefault('timeout', _WARMUP_TIMEOUT_SECONDS)
else:
kwargs.setdefault('timeout', _API_REQUEST_TIMEOUT_SECONDS)
last_error = None