refactor(reservation): 抽 attend_window 的"锁定期处理"块到 process_lock_period
将 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) <noreply@anthropic.com>
This commit is contained in:
1 parent
51d859150d
commit
8fea99a168
2 files changed
+79
-47
No files matched your search
@@ -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, "排序后"):
|
||||
|
||||
@@ -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
|
||||
Reference in new issue
Block a user