fix: 增加MySQL重试机制中的错误代码1205并调整等待时间
This commit is contained in:
parent
4b16bd0cc4
commit
ecfd2d227a
11
DB.py
11
DB.py
@ -92,15 +92,14 @@ video_author = Table(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def mysql_retry(max_retries: int = 3, base_delay: float = 1.0):
|
def mysql_retry(max_retries: int = 3, base_delay: float = 10):
|
||||||
RETRIABLE_ERRORS = {2013, 1213, 2006}
|
RETRIABLE_ERRORS = {2013, 1213, 2006, 1205} # 增加 1205
|
||||||
|
|
||||||
def decorator(fn):
|
def decorator(fn):
|
||||||
@functools.wraps(fn)
|
@functools.wraps(fn)
|
||||||
def wrapper(self, *args, **kwargs):
|
def wrapper(self, *args, **kwargs):
|
||||||
for attempt in range(1, max_retries + 1):
|
for attempt in range(1, max_retries + 1):
|
||||||
try:
|
try:
|
||||||
# 确保连接仍存活,失败自动 reconnect
|
|
||||||
self.conn.ping(reconnect=True)
|
self.conn.ping(reconnect=True)
|
||||||
return fn(self, *args, **kwargs)
|
return fn(self, *args, **kwargs)
|
||||||
|
|
||||||
@ -113,15 +112,19 @@ def mysql_retry(max_retries: int = 3, base_delay: float = 1.0):
|
|||||||
2013: "连接断开",
|
2013: "连接断开",
|
||||||
1213: "死锁冲突",
|
1213: "死锁冲突",
|
||||||
2006: "连接失效",
|
2006: "连接失效",
|
||||||
|
1205: "锁等待超时",
|
||||||
}.get(errno, f"MySQL错误{errno}")
|
}.get(errno, f"MySQL错误{errno}")
|
||||||
|
|
||||||
wait = base_delay * (2 ** (attempt - 1))
|
wait = base_delay * (2 ** (attempt - 1))
|
||||||
logger.warning(
|
logger.warning(
|
||||||
f"[MySQL][{fn.__name__}] 第{attempt}次重试({errno} {reason}):{e},等待 {wait:.1f}s...")
|
f"[MySQL][{fn.__name__}] 第{attempt}次重试({errno} {reason}):{e},等待 {wait:.1f}s...")
|
||||||
|
|
||||||
# 仅对断连类错误尝试重连
|
|
||||||
if errno in {2013, 2006}:
|
if errno in {2013, 2006}:
|
||||||
self._reconnect_mysql()
|
self._reconnect_mysql()
|
||||||
|
if errno == 1205:
|
||||||
|
wait = base_delay + 5 # 比如锁等待固定加一点延迟
|
||||||
|
else:
|
||||||
|
wait = base_delay * (2 ** (attempt - 1))
|
||||||
|
|
||||||
time.sleep(wait)
|
time.sleep(wait)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user