From 51d859150de42fd68c54e720b841e0cf049fb7c0 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 10:30:32 +0800 Subject: [PATCH] =?UTF-8?q?refactor(reservation):=20=E6=8A=BD=20attend=5Fw?= =?UTF-8?q?indow=20=E4=B8=AD=E9=87=8D=E5=A4=8D=E7=9A=84"=E8=B6=85=E6=97=B6?= =?UTF-8?q?=E6=A3=80=E6=9F=A5"=E6=A8=A1=E5=BC=8F=E5=88=B0=20check=5Ftotal?= =?UTF-8?q?=5Ftimeout?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit process_reservation_attend_window 内有 2 处结构完全相同的超时检查块 ("排序前"和"排序后"),各 13 行。统一抽到 check_total_timeout helper, 通过 stage_label 参数区分日志。 attend_window.py: 1372 → 1352 行 (-20) attend_window_helpers.py: 116 → 143 行 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) --- .../reservation_logic/attend_window.py | 32 ++++--------------- .../attend_window_helpers.py | 27 ++++++++++++++++ 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/douyin/reservation/reservation_logic/attend_window.py b/douyin/reservation/reservation_logic/attend_window.py index ecde5eb..621fd79 100644 --- a/douyin/reservation/reservation_logic/attend_window.py +++ b/douyin/reservation/reservation_logic/attend_window.py @@ -149,19 +149,10 @@ def process_reservation_attend_window( pass # 超时检查 - try: - if (time.time() - start_time) > total_timeout_s: - try: - analyser._log_kv( - "预约", - "[预约] process_reservation_attend_window:超时退出(排序前)", - total_timeout_seconds=round(total_timeout_s, 3), - ) - except Exception: - pass - return False - except Exception: - pass + from .attend_window_helpers import check_total_timeout + + if check_total_timeout(analyser, start_time, total_timeout_s, "排序前"): + return False try: _sort_reservations_inplace(reservations) except Exception as e: @@ -249,19 +240,8 @@ def process_reservation_attend_window( pass # 超时检查 - try: - if (time.time() - start_time) > total_timeout_s: - try: - analyser._log_kv( - "预约", - "[预约] process_reservation_attend_window:超时退出(排序后)", - total_timeout_seconds=round(total_timeout_s, 3), - ) - except Exception: - pass - return False - except Exception: - pass + if check_total_timeout(analyser, start_time, total_timeout_s, "排序后"): + return False # 取队头(最早需要处理的一条;若在锁定期内已将锁定目标移动到队头,则这里即为锁定目标) target = reservations[0] diff --git a/douyin/reservation/reservation_logic/attend_window_helpers.py b/douyin/reservation/reservation_logic/attend_window_helpers.py index 2d68b89..7306032 100644 --- a/douyin/reservation/reservation_logic/attend_window_helpers.py +++ b/douyin/reservation/reservation_logic/attend_window_helpers.py @@ -1,11 +1,13 @@ """attend_window 内部辅助函数。 从 process_reservation_attend_window 中抽出的相对独立的早返回块。 +- check_total_timeout:检查总超时是否达到,达到则记日志返回 True - handle_already_participated:检测到本预约 future_ts 已被参与,移除队头并写状态 - handle_invalid_future_ts:future_ts 无效,丢弃队头并写状态 """ from __future__ import annotations +import time from typing import Any, Callable, List from ._common import ( @@ -16,6 +18,31 @@ from ._common import ( ) +def check_total_timeout( + analyser: Any, start_time: float, total_timeout_s: float, stage_label: str +) -> bool: + """检查从 start_time 到现在是否超过 total_timeout_s。 + + 超时则写入日志并返回 True;否则返回 False。 + 任何内部异常都被吞噬(与原代码一致),返回 False。 + """ + try: + if (time.time() - start_time) > total_timeout_s: + try: + analyser._log_kv( + "预约", + f"[预约] process_reservation_attend_window:超时退出({stage_label})", + total_timeout_seconds=round(total_timeout_s, 3), + ) + except Exception: + pass + return True + except Exception: + pass + return False + + + def _write_state_unified( write_state_callback: Callable[[], None], analyser: Any,