From 6c49ef042e370c7b37f7f4fb8bf725fe586d3171 Mon Sep 17 00:00:00 2001 From: Franklin-F Date: Mon, 14 Jul 2025 19:08:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8D=95=E8=8E=B7=E6=9B=B4=E5=A4=9A?= =?UTF-8?q?=E6=96=AD=E7=BA=BF=E5=BC=82=E5=B8=B8=E5=B9=B6=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E9=87=8D=E8=AF=95=E9=80=BB=E8=BE=91=EF=BC=8C=E7=A1=AE=E4=BF=9D?= =?UTF-8?q?=E6=9B=B4=E7=A8=B3=E5=81=A5=E7=9A=84=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DB.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/DB.py b/DB.py index 68f2e0d..c9815d3 100644 --- a/DB.py +++ b/DB.py @@ -94,8 +94,7 @@ video_author = Table( def mysql_retry(max_retries: int = 3, base_delay: float = 2.0): """ - 装饰器工厂:捕获 InterfaceError 后断线重连并重试, - 重试间隔按指数级增长:base_delay * 2**(attempt-1) 秒。 + 装饰器:捕获断线异常(InterfaceError 或 OperationalError: 2013)后尝试重连,指数回退重试。 """ def decorator(fn): @@ -103,16 +102,17 @@ def mysql_retry(max_retries: int = 3, base_delay: float = 2.0): def wrapper(self, *args, **kwargs): for attempt in range(1, max_retries + 1): try: - # 确保连接存活,reconnect=True 会在 ping 失败时重连 self.conn.ping(reconnect=True) return fn(self, *args, **kwargs) - except pymysql.InterfaceError as e: + except (pymysql.InterfaceError, pymysql.OperationalError) as e: + if isinstance(e, pymysql.OperationalError) and e.args[0] != 2013: + raise # 只处理 2013,其他 OperationalError 抛出 wait = base_delay * (2 ** (attempt - 1)) - logger.info(f"[MySQL][{fn.__name__}] 第{attempt}次 InterfaceError:{e},等待 {wait:.1f}s 后重连…") + logger.warning(f"[MySQL][{fn.__name__}] 第{attempt}次重试(断开连接:{e}),等待 {wait:.1f}s 后重连…") time.sleep(wait) self._reconnect_mysql() if attempt == max_retries: - logger.info("[MySQL] 重试多次仍失败,抛出异常") + logger.error("[MySQL] 重试多次仍失败,抛出异常") raise return wrapper