perf: 启动预热优化 - 解决容器重启后首批任务慢/失败
问题:容器重启后前两批任务明显变慢或失败 - 第一批:代理/目标服务器连接冷启动导致超时 - 第二批:浏览器池冷启动需要创建浏览器 解决方案: - browser_pool_worker.py: 添加 pre_warm 参数,启动时预创建1个浏览器 - api_browser.py: 添加 warmup_api_connection() 预热 TCP/TLS 连接 - api_browser.py: 首次请求使用更长超时(10s),后续恢复正常 - app.py: 启动时后台调用 API 预热 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -71,6 +71,7 @@ class APIBrowser:
|
||||
self.log_callback = log_callback
|
||||
self.stop_flag = False
|
||||
self._closed = False # 防止重复关闭
|
||||
self._first_request = True
|
||||
self.last_total_records = 0
|
||||
|
||||
# 设置代理
|
||||
@@ -132,7 +133,12 @@ class APIBrowser:
|
||||
|
||||
def _request_with_retry(self, method, url, max_retries=3, retry_delay=1, **kwargs):
|
||||
"""带重试机制的请求方法"""
|
||||
kwargs.setdefault('timeout', _API_REQUEST_TIMEOUT_SECONDS)
|
||||
# 首次请求使用更长超时(10秒),后续使用配置的超时
|
||||
if self._first_request:
|
||||
kwargs.setdefault('timeout', 10.0)
|
||||
self._first_request = False
|
||||
else:
|
||||
kwargs.setdefault('timeout', _API_REQUEST_TIMEOUT_SECONDS)
|
||||
last_error = None
|
||||
|
||||
for attempt in range(1, max_retries + 1):
|
||||
@@ -518,3 +524,28 @@ class APIBrowser:
|
||||
"""Context manager支持 - 退出"""
|
||||
self.close()
|
||||
return False # 不抑制异常
|
||||
|
||||
|
||||
def warmup_api_connection(proxy_config: Optional[dict] = None, log_callback: Optional[Callable] = None):
|
||||
"""预热 API 连接 - 建立 TCP/TLS 连接池"""
|
||||
|
||||
def log(msg: str):
|
||||
if log_callback:
|
||||
log_callback(msg)
|
||||
else:
|
||||
print(f"[API预热] {msg}")
|
||||
|
||||
log("正在预热 API 连接...")
|
||||
try:
|
||||
session = requests.Session()
|
||||
if proxy_config and proxy_config.get("server"):
|
||||
session.proxies = {"http": proxy_config["server"], "https": proxy_config["server"]}
|
||||
|
||||
# 发送一个轻量级请求建立连接
|
||||
resp = session.get(f"{BASE_URL}/admin/login.aspx", timeout=10, allow_redirects=False)
|
||||
log(f"✓ API 连接预热完成 (status={resp.status_code})")
|
||||
session.close()
|
||||
return True
|
||||
except Exception as e:
|
||||
log(f"API 连接预热失败: {e}")
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user