From ecfd2d227af68e5c223782e8d3538f8c1f80dbb1 Mon Sep 17 00:00:00 2001 From: Franklin-F Date: Wed, 23 Jul 2025 20:31:21 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=A2=9E=E5=8A=A0MySQL=E9=87=8D?= =?UTF-8?q?=E8=AF=95=E6=9C=BA=E5=88=B6=E4=B8=AD=E7=9A=84=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E4=BB=A3=E7=A0=811205=E5=B9=B6=E8=B0=83=E6=95=B4=E7=AD=89?= =?UTF-8?q?=E5=BE=85=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DB.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/DB.py b/DB.py index 69ffdf0..4aa1b8e 100644 --- a/DB.py +++ b/DB.py @@ -92,15 +92,14 @@ video_author = Table( ) -def mysql_retry(max_retries: int = 3, base_delay: float = 1.0): - RETRIABLE_ERRORS = {2013, 1213, 2006} +def mysql_retry(max_retries: int = 3, base_delay: float = 10): + RETRIABLE_ERRORS = {2013, 1213, 2006, 1205} # 增加 1205 def decorator(fn): @functools.wraps(fn) def wrapper(self, *args, **kwargs): for attempt in range(1, max_retries + 1): try: - # 确保连接仍存活,失败自动 reconnect self.conn.ping(reconnect=True) return fn(self, *args, **kwargs) @@ -113,15 +112,19 @@ def mysql_retry(max_retries: int = 3, base_delay: float = 1.0): 2013: "连接断开", 1213: "死锁冲突", 2006: "连接失效", + 1205: "锁等待超时", }.get(errno, f"MySQL错误{errno}") wait = base_delay * (2 ** (attempt - 1)) logger.warning( f"[MySQL][{fn.__name__}] 第{attempt}次重试({errno} {reason}):{e},等待 {wait:.1f}s...") - # 仅对断连类错误尝试重连 if errno in {2013, 2006}: self._reconnect_mysql() + if errno == 1205: + wait = base_delay + 5 # 比如锁等待固定加一点延迟 + else: + wait = base_delay * (2 ** (attempt - 1)) time.sleep(wait)