优化:Redis 连接增强 — TCP keepalive 适配 + 截图处理器独立重连

- 新增 _build_keepalive_options() 适配 Linux/Windows TCP keepalive
- health_check_interval 30→15秒,更快发现断连
- 截图处理器新增 _reconnect_cloud_redis() 独立重连能力
- 截图监听捕获 ConnectionError 主动重连,不再退避到60秒
This commit is contained in:
2026-03-20 11:19:31 +08:00
parent d5c443c7c6
commit b70f8cd680
3 changed files with 59 additions and 2 deletions

View File

@@ -14,6 +14,8 @@
import json
import logging
import os
import platform
import socket
import threading
import time
from datetime import datetime
@@ -30,6 +32,23 @@ from utils.version_control import get_version_control
logger = logging.getLogger(__name__)
def _build_keepalive_options():
"""构建 TCP keepalive 选项,适配 Linux/Windows"""
opts = {}
if platform.system() == "Linux":
# TCP_KEEPIDLE: 连接空闲 15s 后开始发送 keepalive 探测
# TCP_KEEPINTVL: 每次探测间隔 5s
# TCP_KEEPCNT: 连续 3 次探测失败则判定断连
opts = {
socket.TCP_KEEPIDLE: 15,
socket.TCP_KEEPINTVL: 5,
socket.TCP_KEEPCNT: 3,
}
# Windows 不支持 TCP_KEEPIDLE/KEEPINTVL/KEEPCNT
# 但 socket_keepalive=True 仍会启用默认 keepalive
return opts
# ==================== Redis Key 常量 ====================
# 云端 Redis Keys
@@ -207,7 +226,8 @@ class ConfigSyncManager:
socket_timeout=10,
retry_on_timeout=True,
socket_keepalive=True,
health_check_interval=30,
socket_keepalive_options=_build_keepalive_options(),
health_check_interval=15,
)
self._cloud_redis.ping()
logger.info(f"云端 Redis 连接成功: {cfg.host}:{cfg.port}/{cfg.db}")