fix(自动还车): 失败重试改为密集曲线,不再拖 5~10 分钟才还上
现场:51181(order 20656388)第一次 /record/return 报「关锁失败!」,第二次成功, 晚了 4.2 分钟;51291(order 20656387)晚了 24.5 分钟。 旧曲线是 30s×6 之后 60/120/300/600s 封顶 —— 一次硬件侧关锁失败就要付 3~10 分钟的迟到,24.5 分钟正好是「6 次快保 + 数次退避」的结果。 改法 - 新增密集表 AUTO_RETURN_RETRY_DELAYS = (10,10,15,15,20,20,30,30,30,45,60): 前 11 次按 10~60 秒递进,先在原定时刻附近多试几次 - 之后只要距首次失败还没超过 AUTO_RETURN_DENSE_SECONDS(30 分钟), 间隔**恒定 60 秒**(用户口径:不允许超过 1 分钟的时间窗口才还上) - 超过 30 分钟才允许 60/120/300 秒,封顶 5 分钟;6 小时绝对上限不变 - _auto_return_retry_delay(failures, elapsed_seconds=None) 新增 elapsed 参数, 调用处传 now - first_fail;传 None 时按"窗口内"处理(保守多试一次) - 保留 AUTO_RETURN_RETRY_DELAY / AUTO_RETURN_MAX_RETRIES 旧名(兼容外部引用) - 快保阶段的判定由「次数 < MAX_RETRIES」改为「次数 < len(密集表)」 效果对比(首次失败后累计等待) 旧:30,60,90,120,150,180 → 240 → 360 → 660 → 1260 … 新:10,20,35,50,70,90 → 120 → 150 → 180 → 225 → 285 → +60s/次 … 即:≤3 分钟内的失败都能在 1 分钟内重试掉,不再出现"晚 5~25 分钟" 测试:test_rental_dedup 的延迟序列/快保断言同步新曲线;全量 238 passed + 44 node
This commit is contained in:
@@ -272,10 +272,15 @@ class RentalDedupTests(unittest.TestCase):
|
|||||||
|
|
||||||
# ---------- BUG-07 ----------
|
# ---------- BUG-07 ----------
|
||||||
def test_retry_delay_backoff_sequence(self):
|
def test_retry_delay_backoff_sequence(self):
|
||||||
|
"""失败后重试间隔:前 11 次 10~60s 递进,之后窗口内长期 60s。"""
|
||||||
delays = [timer._auto_return_retry_delay(n) for n in range(10)]
|
delays = [timer._auto_return_retry_delay(n) for n in range(10)]
|
||||||
self.assertEqual([30, 30, 30, 30, 30, 30, 60, 120, 300, 600], delays)
|
self.assertEqual([10, 10, 15, 15, 20, 20, 30, 30, 30, 45], delays)
|
||||||
self.assertEqual(600, timer._auto_return_retry_delay(50)) # 封顶 10 分钟
|
# 30 分钟窗口内:一律 ≤60 秒(用户口径:不能超过 1 分钟才还上)
|
||||||
self.assertEqual(30, timer._auto_return_retry_delay(None)) # 脏值不炸
|
self.assertEqual(60, timer._auto_return_retry_delay(50))
|
||||||
|
self.assertEqual(60, timer._auto_return_retry_delay(50, 29 * 60))
|
||||||
|
# 超过窗口才允许拉长,封顶 5 分钟
|
||||||
|
self.assertEqual(300, timer._auto_return_retry_delay(50, 3600))
|
||||||
|
self.assertEqual(10, timer._auto_return_retry_delay(None)) # 脏值不炸
|
||||||
|
|
||||||
def test_backoff_phase_schedules_no_timer_and_alerts_once_per_level(self):
|
def test_backoff_phase_schedules_no_timer_and_alerts_once_per_level(self):
|
||||||
timer._auto_return_retries[55] = timer.AUTO_RETURN_MAX_RETRIES
|
timer._auto_return_retries[55] = timer.AUTO_RETURN_MAX_RETRIES
|
||||||
@@ -294,13 +299,13 @@ class RentalDedupTests(unittest.TestCase):
|
|||||||
timer._schedule_auto_return_retry(55, "r@example.test", BIKE, 7, "boom")
|
timer._schedule_auto_return_retry(55, "r@example.test", BIKE, 7, "boom")
|
||||||
self.assertEqual(1, alert.call_count)
|
self.assertEqual(1, alert.call_count)
|
||||||
|
|
||||||
# 快保阶段仍然挂 30s Timer
|
# 快保阶段仍然挂 Timer(第 2 次失败后的间隔是密集表里的 10s)
|
||||||
timer._auto_return_retries[56] = 1
|
timer._auto_return_retries[56] = 1
|
||||||
with patch.object(timer, "_alert_auto_return_exhausted") as alert2, \
|
with patch.object(timer, "_alert_auto_return_exhausted") as alert2, \
|
||||||
patch.object(timer, "_schedule_reminder") as schedule2:
|
patch.object(timer, "_schedule_reminder") as schedule2:
|
||||||
timer._schedule_auto_return_retry(56, "r@example.test", BIKE, 7, "boom")
|
timer._schedule_auto_return_retry(56, "r@example.test", BIKE, 7, "boom")
|
||||||
alert2.assert_not_called()
|
alert2.assert_not_called()
|
||||||
self.assertEqual(30, schedule2.call_args.args[1])
|
self.assertEqual(timer.AUTO_RETURN_RETRY_DELAYS[1], schedule2.call_args.args[1])
|
||||||
|
|
||||||
def test_recover_throttle_honours_backoff_until(self):
|
def test_recover_throttle_honours_backoff_until(self):
|
||||||
timer._auto_return_backoff_until[77] = timer._time.time() + 120
|
timer._auto_return_backoff_until[77] = timer._time.time() + 120
|
||||||
|
|||||||
@@ -913,20 +913,39 @@ _auto_return_first_fail_at: dict = {} # rental_id -> 首次自动还车失
|
|||||||
# 绝对上限:再"不放弃"也不能对着一个永久失败的单无限重试 —— 每 10 分钟打一次上游、
|
# 绝对上限:再"不放弃"也不能对着一个永久失败的单无限重试 —— 每 10 分钟打一次上游、
|
||||||
# 刷一条日志,线上排查时全是噪音。到点发一封告警后停手,等人工或页面重试兜底。
|
# 刷一条日志,线上排查时全是噪音。到点发一封告警后停手,等人工或页面重试兜底。
|
||||||
AUTO_RETURN_GIVE_UP_SECONDS = 6 * 3600
|
AUTO_RETURN_GIVE_UP_SECONDS = 6 * 3600
|
||||||
AUTO_RETURN_RETRY_DELAY = 30 # 秒,快保阶段间隔
|
# 到点还车失败的密集重试曲线(秒)。用户口径:不能超过 1 分钟的时间窗口才还上,
|
||||||
AUTO_RETURN_MAX_RETRIES = 6 # 快保次数(30s × 6)
|
# 所以前 5 分钟按 10~60 秒递进,之后**长期保持 60 秒**,只有拖了半小时以上才允许
|
||||||
AUTO_RETURN_BACKOFF_DELAYS = (60, 120, 300, 600) # 退避阶段间隔,末位即封顶(10 分钟)
|
# 拉长到 2/5 分钟(避免对同一张单无限高频打上游)。
|
||||||
|
AUTO_RETURN_RETRY_DELAYS = (10, 10, 15, 15, 20, 20, 30, 30, 30, 45, 60)
|
||||||
|
AUTO_RETURN_DENSE_SECONDS = 30 * 60 # 这个时间窗内重试间隔一律 ≤ 60 秒
|
||||||
|
AUTO_RETURN_BACKOFF_DELAYS = (60, 120, 300) # 超过窗口后才逐步拉长,末位封顶 5 分钟
|
||||||
|
# 兼容旧名字(外部/测试可能引用)
|
||||||
|
AUTO_RETURN_RETRY_DELAY = AUTO_RETURN_RETRY_DELAYS[0]
|
||||||
|
AUTO_RETURN_MAX_RETRIES = len(AUTO_RETURN_RETRY_DELAYS)
|
||||||
|
|
||||||
|
|
||||||
def _auto_return_retry_delay(failures: int) -> int:
|
def _auto_return_retry_delay(failures: int, elapsed_seconds=None) -> int:
|
||||||
"""第 failures+1 次重试前的等待秒数(failures = 已经失败的次数)。
|
"""第 failures+1 次重试前要等多少秒。
|
||||||
|
|
||||||
0..5 → 30s(快保,尽量准点);6.. → 60/120/300/600s,600s 封顶。
|
- 前 len(AUTO_RETURN_RETRY_DELAYS) 次:按 10/10/15/15/20/20/30/30/30/45/60 递进;
|
||||||
|
- 之后:只要从首次失败算起还没超过 AUTO_RETURN_DENSE_SECONDS,就一直 60 秒;
|
||||||
|
- 超过窗口(默认 30 分钟)才允许 2/5 分钟,避免长期高频打上游。
|
||||||
|
|
||||||
|
elapsed_seconds 传 None 时按"窗口内"处理(保守:宁可多试一次)。
|
||||||
"""
|
"""
|
||||||
n = _coerce_int(failures, 0)
|
n = _coerce_int(failures, 0)
|
||||||
if n < AUTO_RETURN_MAX_RETRIES:
|
if n < len(AUTO_RETURN_RETRY_DELAYS):
|
||||||
return AUTO_RETURN_RETRY_DELAY
|
return AUTO_RETURN_RETRY_DELAYS[n]
|
||||||
idx = min(n - AUTO_RETURN_MAX_RETRIES, len(AUTO_RETURN_BACKOFF_DELAYS) - 1)
|
elapsed = None
|
||||||
|
if elapsed_seconds is not None:
|
||||||
|
try:
|
||||||
|
elapsed = float(elapsed_seconds)
|
||||||
|
except (TypeError, ValueError):
|
||||||
|
elapsed = None
|
||||||
|
if elapsed is None or elapsed <= AUTO_RETURN_DENSE_SECONDS:
|
||||||
|
return AUTO_RETURN_BACKOFF_DELAYS[0] # 60 秒
|
||||||
|
over = n - len(AUTO_RETURN_RETRY_DELAYS)
|
||||||
|
idx = min(over, len(AUTO_RETURN_BACKOFF_DELAYS) - 1)
|
||||||
return AUTO_RETURN_BACKOFF_DELAYS[idx]
|
return AUTO_RETURN_BACKOFF_DELAYS[idx]
|
||||||
|
|
||||||
|
|
||||||
@@ -1062,7 +1081,7 @@ def _schedule_auto_return_retry(rental_id: int, email: str, bike_code: str, user
|
|||||||
return
|
return
|
||||||
|
|
||||||
n = _auto_return_retries.get(rental_id, 0)
|
n = _auto_return_retries.get(rental_id, 0)
|
||||||
delay = _auto_return_retry_delay(n)
|
delay = _auto_return_retry_delay(n, now_ts - first_fail)
|
||||||
_auto_return_retries[rental_id] = n + 1
|
_auto_return_retries[rental_id] = n + 1
|
||||||
level = _auto_return_alert_level(n + 1)
|
level = _auto_return_alert_level(n + 1)
|
||||||
if level > _auto_return_alert_levels.get(rental_id, 0):
|
if level > _auto_return_alert_levels.get(rental_id, 0):
|
||||||
@@ -1070,7 +1089,7 @@ def _schedule_auto_return_retry(rental_id: int, email: str, bike_code: str, user
|
|||||||
_alert_auto_return_exhausted(rental_id, bike_code or _rental_contact(rental_id)[0],
|
_alert_auto_return_exhausted(rental_id, bike_code or _rental_contact(rental_id)[0],
|
||||||
message, delay)
|
message, delay)
|
||||||
|
|
||||||
if n < AUTO_RETURN_MAX_RETRIES:
|
if n < len(AUTO_RETURN_RETRY_DELAYS):
|
||||||
# 快保阶段:进程内 Timer 精确重试,尽量在原定时刻附近把车还掉
|
# 快保阶段:进程内 Timer 精确重试,尽量在原定时刻附近把车还掉
|
||||||
print(f"[自动还车] rental_id={rental_id} 第 {n + 1} 次重试将在 {delay}s 后执行")
|
print(f"[自动还车] rental_id={rental_id} 第 {n + 1} 次重试将在 {delay}s 后执行")
|
||||||
_schedule_reminder(rental_id, delay, "0", email, bike_code,
|
_schedule_reminder(rental_id, delay, "0", email, bike_code,
|
||||||
|
|||||||
Reference in New Issue
Block a user