From 8fea99a16852fcc9405e7cc12bf3c0b7b67540b4 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 10:31:53 +0800 Subject: [PATCH] =?UTF-8?q?refactor(reservation):=20=E6=8A=BD=20attend=5Fw?= =?UTF-8?q?indow=20=E7=9A=84"=E9=94=81=E5=AE=9A=E6=9C=9F=E5=A4=84=E7=90=86?= =?UTF-8?q?"=E5=9D=97=E5=88=B0=20process=5Flock=5Fperiod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 process_reservation_attend_window 中"锁定期内:优先处理锁定的预约" 独立块(51 行)抽到 process_lock_period helper。逻辑: - 在 reservations 中找与 focus_until 对应的预约(误差 ≤5 秒) - 找不到 → 视为锁定失效,清除 focus_until_ts_ref,返回 0.0 - 找到但不在队头 → 移到队头,返回原 focus_until - 已在队头 → 不动,返回原 focus_until attend_window.py: 1352 → 1314 行 (-38) attend_window_helpers.py: 143 → 213 行 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) --- .../reservation_logic/attend_window.py | 56 +++------------ .../attend_window_helpers.py | 70 +++++++++++++++++++ 2 files changed, 79 insertions(+), 47 deletions(-) diff --git a/douyin/reservation/reservation_logic/attend_window.py b/douyin/reservation/reservation_logic/attend_window.py index 621fd79..fe23948 100644 --- a/douyin/reservation/reservation_logic/attend_window.py +++ b/douyin/reservation/reservation_logic/attend_window.py @@ -190,54 +190,16 @@ def process_reservation_attend_window( # 锁定期内:优先处理“锁定的预约”,避免被队头其它预约打断。 # 同时,如果锁定期对应的预约已不在队列(例如刚参与成功并移除),视为“锁定失效”,清除锁定期。 if focus_until > 0 and now_ts < focus_until: - locked_idx = None - try: - for i, item in enumerate(reservations): - try: - item_ft = float(item.get("future_ts") or 0.0) - except Exception: - continue - if item_ft <= 0: - continue - expected_focus_until = item_ft + max(0.0, poll_before) - if abs(focus_until - expected_focus_until) <= 5.0: - locked_idx = i - break - except Exception: - locked_idx = None + from .attend_window_helpers import process_lock_period - if locked_idx is None: - try: - analyser._log_kv( - "预约", - "[预约] 检测到锁定期已失效(锁定预约不在队列),清除锁定期", - focus_until=int(focus_until), - lock_period_remain_seconds=int(focus_until - now_ts), - reservations_count=len(reservations), - ) - except Exception: - pass - try: - if reservation_focus_until_ts_ref: - reservation_focus_until_ts_ref[0] = 0.0 - else: - reservation_focus_until_ts_ref.append(0.0) - except Exception: - pass - focus_until = 0.0 - elif locked_idx != 0: - try: - locked_item = reservations.pop(locked_idx) - reservations.insert(0, locked_item) - analyser._log_kv( - "预约", - "[预约] 锁定期内固定处理锁定预约(忽略队头其他预约)", - focus_until=int(focus_until), - locked_future_ts=int(float(locked_item.get("future_ts") or 0.0)), - moved_from_index=int(locked_idx), - ) - except Exception: - pass + focus_until = process_lock_period( + analyser, + reservations, + focus_until, + now_ts, + poll_before, + reservation_focus_until_ts_ref, + ) # 超时检查 if check_total_timeout(analyser, start_time, total_timeout_s, "排序后"): diff --git a/douyin/reservation/reservation_logic/attend_window_helpers.py b/douyin/reservation/reservation_logic/attend_window_helpers.py index 7306032..a35c6b5 100644 --- a/douyin/reservation/reservation_logic/attend_window_helpers.py +++ b/douyin/reservation/reservation_logic/attend_window_helpers.py @@ -141,3 +141,73 @@ def handle_invalid_future_ts( _write_state_unified( write_state_callback, analyser, target, reservations, "future_ts 无效" ) + + +def process_lock_period( + analyser: Any, + reservations: List[ReservationItem], + focus_until: float, + now_ts: float, + poll_before: float, + reservation_focus_until_ts_ref: List[float], +) -> float: + """锁定期内:优先处理"锁定的预约",避免被队头其它预约打断。 + + - 找到与 focus_until 对应的预约(误差 ≤ 5 秒); + - 找不到 → 视为锁定失效,清除 focus_until_ts_ref; + - 找到但不在队头 → 把该项移到队头; + - 已在队头 → 不动。 + + 返回(可能更新过的)focus_until。 + """ + locked_idx = None + try: + for i, item in enumerate(reservations): + try: + item_ft = float(item.get("future_ts") or 0.0) + except Exception: + continue + if item_ft <= 0: + continue + expected_focus_until = item_ft + max(0.0, poll_before) + if abs(focus_until - expected_focus_until) <= 5.0: + locked_idx = i + break + except Exception: + locked_idx = None + + if locked_idx is None: + try: + analyser._log_kv( + "预约", + "[预约] 检测到锁定期已失效(锁定预约不在队列),清除锁定期", + focus_until=int(focus_until), + lock_period_remain_seconds=int(focus_until - now_ts), + reservations_count=len(reservations), + ) + except Exception: + pass + try: + if reservation_focus_until_ts_ref: + reservation_focus_until_ts_ref[0] = 0.0 + else: + reservation_focus_until_ts_ref.append(0.0) + except Exception: + pass + return 0.0 + + if locked_idx != 0: + try: + locked_item = reservations.pop(locked_idx) + reservations.insert(0, locked_item) + analyser._log_kv( + "预约", + "[预约] 锁定期内固定处理锁定预约(忽略队头其他预约)", + focus_until=int(focus_until), + locked_future_ts=int(float(locked_item.get("future_ts") or 0.0)), + moved_from_index=int(locked_idx), + ) + except Exception: + pass + + return focus_until