refactor(reservation): 抽 attend_window 中重复的"超时检查"模式到 check_total_timeout
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) <noreply@anthropic.com>
This commit is contained in:
1 parent
c509bf1262
commit
51d859150d
2 files changed
+33
-26
No files matched your search
@@ -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]
|
||||
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user