diff --git a/tests/test_rental_dedup.py b/tests/test_rental_dedup.py index 16064f9..6effee4 100644 --- a/tests/test_rental_dedup.py +++ b/tests/test_rental_dedup.py @@ -272,10 +272,15 @@ class RentalDedupTests(unittest.TestCase): # ---------- BUG-07 ---------- def test_retry_delay_backoff_sequence(self): + """失败后重试间隔:前 11 次 10~60s 递进,之后窗口内长期 60s。""" 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(600, timer._auto_return_retry_delay(50)) # 封顶 10 分钟 - self.assertEqual(30, timer._auto_return_retry_delay(None)) # 脏值不炸 + self.assertEqual([10, 10, 15, 15, 20, 20, 30, 30, 30, 45], delays) + # 30 分钟窗口内:一律 ≤60 秒(用户口径:不能超过 1 分钟才还上) + 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): 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") self.assertEqual(1, alert.call_count) - # 快保阶段仍然挂 30s Timer + # 快保阶段仍然挂 Timer(第 2 次失败后的间隔是密集表里的 10s) timer._auto_return_retries[56] = 1 with patch.object(timer, "_alert_auto_return_exhausted") as alert2, \ patch.object(timer, "_schedule_reminder") as schedule2: timer._schedule_auto_return_retry(56, "r@example.test", BIKE, 7, "boom") 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): timer._auto_return_backoff_until[77] = timer._time.time() + 120 diff --git a/timer.py b/timer.py index 4830d02..2344c26 100644 --- a/timer.py +++ b/timer.py @@ -913,20 +913,39 @@ _auto_return_first_fail_at: dict = {} # rental_id -> 首次自动还车失 # 绝对上限:再"不放弃"也不能对着一个永久失败的单无限重试 —— 每 10 分钟打一次上游、 # 刷一条日志,线上排查时全是噪音。到点发一封告警后停手,等人工或页面重试兜底。 AUTO_RETURN_GIVE_UP_SECONDS = 6 * 3600 -AUTO_RETURN_RETRY_DELAY = 30 # 秒,快保阶段间隔 -AUTO_RETURN_MAX_RETRIES = 6 # 快保次数(30s × 6) -AUTO_RETURN_BACKOFF_DELAYS = (60, 120, 300, 600) # 退避阶段间隔,末位即封顶(10 分钟) +# 到点还车失败的密集重试曲线(秒)。用户口径:不能超过 1 分钟的时间窗口才还上, +# 所以前 5 分钟按 10~60 秒递进,之后**长期保持 60 秒**,只有拖了半小时以上才允许 +# 拉长到 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: - """第 failures+1 次重试前的等待秒数(failures = 已经失败的次数)。 +def _auto_return_retry_delay(failures: int, elapsed_seconds=None) -> int: + """第 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) - if n < AUTO_RETURN_MAX_RETRIES: - return AUTO_RETURN_RETRY_DELAY - idx = min(n - AUTO_RETURN_MAX_RETRIES, len(AUTO_RETURN_BACKOFF_DELAYS) - 1) + if n < len(AUTO_RETURN_RETRY_DELAYS): + return AUTO_RETURN_RETRY_DELAYS[n] + 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] @@ -1062,7 +1081,7 @@ def _schedule_auto_return_retry(rental_id: int, email: str, bike_code: str, user return 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 level = _auto_return_alert_level(n + 1) 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], message, delay) - if n < AUTO_RETURN_MAX_RETRIES: + if n < len(AUTO_RETURN_RETRY_DELAYS): # 快保阶段:进程内 Timer 精确重试,尽量在原定时刻附近把车还掉 print(f"[自动还车] rental_id={rental_id} 第 {n + 1} 次重试将在 {delay}s 后执行") _schedule_reminder(rental_id, delay, "0", email, bike_code,