feat: 分时段轮询(营业 2s / 出单后 1.5s 追击 / 非营业 8s)+ 端到端延迟指标

- 实测订单接口平均 300ms,2.5 次/秒连续 20 次无限流,故营业时段可压到 2 秒
- 商家后台 JS 无 WebSocket/SSE/socket.io/MQTT(0 命中)→ 平台不推,只能轮询
- 出单后 60 秒内自动加密到 1.5 秒(出单常成串),窗口结束回落
- 新增 last_detect_lag_sec:以订单 payTime 计算「支付→检出」真实延迟,写入 state 并暴露在 /healthz
- 新增 6 项节奏测试(共 30 项全绿)
- 修复 login_url 折行产生空格、上传内网绕过系统代理、新增 --upload-only 补传
This commit is contained in:
houhuan committed 2026-11-18 14:08:59 +08:00
1 parent 4ad6ecd5c9
commit 88f7183554
7 files changed
+187 -3

No files matched your search

+16
View File
@@ -45,6 +45,12 @@ DEFAULTS: Dict[str, Any] = {
"peak_hours": [[11, 13], [17, 19]],
"interval_min": 5,
"interval_max": 10,
# 分时段轮询(实测接口 ~300ms、2.5 次/秒无限流,故可压到 2 秒)
"business_hours": [[9, 21]], # 营业时段
"interval_business": 2, # 营业时段间隔(秒)
"interval_idle": 8, # 非营业时段间隔(秒)
"interval_burst": 1.5, # 刚出单后的追击间隔(秒)
"burst_window_sec": 60, # 追击持续时间(秒):一单接一单时自动加密
"peak_page_size": 20,
"idle_page_size": 5,
"night_start": "21:00",
@@ -123,6 +129,11 @@ class PollingCfg:
peak_hours: List[Tuple[int, int]]
interval_min: int
interval_max: int
business_hours: List[Tuple[int, int]]
interval_business: float
interval_idle: float
interval_burst: float
burst_window_sec: int
peak_page_size: int
idle_page_size: int
night_start_min: int
@@ -260,6 +271,11 @@ def load_config(path: str | os.PathLike | None = None) -> Config:
peak_hours=[tuple(h) for h in polling["peak_hours"]],
interval_min=int(polling["interval_min"]),
interval_max=int(polling["interval_max"]),
business_hours=[tuple(h) for h in polling.get("business_hours") or [[9, 21]]],
interval_business=float(polling.get("interval_business") or polling["interval_min"]),
interval_idle=float(polling.get("interval_idle") or polling["interval_max"]),
interval_burst=float(polling.get("interval_burst") or 1.5),
burst_window_sec=int(polling.get("burst_window_sec") or 60),
peak_page_size=int(polling["peak_page_size"]),
idle_page_size=int(polling["idle_page_size"]),
night_start_min=parse_hhmm(polling["night_start"]),
+44 -1
View File
@@ -61,6 +61,7 @@ class Monitor:
self._cookie_sig: Optional[tuple] = None
self._last_state_save = 0.0
self._net_failures = 0
self._burst_until = 0.0 # 出单后的高频追击截止时刻
self._started_at = time.time()
self._admin = None
self._last_cookie_check = 0.0
@@ -82,6 +83,11 @@ class Monitor:
hour = self.now().hour
return any(start <= hour < end for start, end in self.cfg.polling.peak_hours)
def is_business(self) -> bool:
"""是否处于营业时段(决定轮询用 2 秒还是 8 秒)。"""
hour = self.now().hour
return any(start <= hour < end for start, end in self.cfg.polling.business_hours)
def is_night(self) -> bool:
n = self.now()
minutes = n.hour * 60 + n.minute
@@ -192,6 +198,9 @@ class Monitor:
"total_pushed": self.state.total_pushed,
"uptime_sec": int(now - self._started_at),
"net_failures": self._net_failures,
"last_detect_lag_sec": self.state.last_detect_lag_sec,
"polling_interval_sec": round(self._next_interval(), 2),
"bursting": time.time() < self._burst_until,
}
def _save_state(self, force: bool = False) -> None:
@@ -405,13 +414,47 @@ class Monitor:
self.speaker.say(msg)
time.sleep(0.3)
# 刚出一单,接下来一小段时间加密轮询(出单常成串)
self._burst_until = time.time() + self.cfg.polling.burst_window_sec
lag = self._detect_lag(new_orders[0])
if lag is not None:
self.state.last_detect_lag_sec = round(lag, 1)
log.info("本单端到端延迟 %.1f 秒(支付→检出),此后 %d 秒内按 %.1f 秒追击",
lag, self.cfg.polling.burst_window_sec, self.cfg.polling.interval_burst)
self.state.mark_pushed([str(o.get("orderId")) for o in new_orders])
self.state.last_order_id = newest_id
self.state.save()
def _detect_lag(self, order: Dict[str, Any]) -> Optional[float]:
"""端到端延迟 = 现在 - 支付时间。
这是唯一能证明「到底多快」的指标:它同时包含平台落单延迟 + 我们的轮询间隔。
超过 24 小时的视为脏数据(时区/补推历史单)直接丢弃。
"""
try:
pay = float(order.get("payTime") or order.get("createTime") or 0)
except (TypeError, ValueError):
return None
if pay <= 0:
return None
lag = time.time() - pay
return lag if 0 <= lag < 86400 else None
def _next_interval(self) -> float:
"""分时段间隔:刚出单后加密,营业时段快,非营业时段慢。
实测订单接口平均 300ms、2.5 次/秒无任何限流,因此营业时段可以压到 2 秒;
刚推完一单后 60 秒内用 1.5 秒(出单常成串),窗口过了自动回落。
"""
p = self.cfg.polling
return random.uniform(p.interval_min, p.interval_max)
if time.time() < self._burst_until:
base = p.interval_burst
elif self.is_business():
base = p.interval_business
else:
base = p.interval_idle
return max(0.5, base + random.uniform(-0.2, 0.2))
def _wait(self, seconds: float) -> None:
"""可中断等待。"""
+4
View File
@@ -32,6 +32,7 @@ class State:
cookie_valid: bool = False
gap_since_ts: float = 0.0 # 最近一次进入「会话失效」的时刻
total_pushed: int = 0
last_detect_lag_sec: Optional[float] = None # 最近一单:支付→检出 的端到端延迟
# 运行时字段,不落盘
_path: Optional[Path] = field(default=None, repr=False, compare=False)
@@ -50,6 +51,8 @@ class State:
state.cookie_valid = bool(data.get("cookie_valid"))
state.gap_since_ts = float(data.get("gap_since_ts") or 0)
state.total_pushed = int(data.get("total_pushed") or 0)
lag = data.get("last_detect_lag_sec")
state.last_detect_lag_sec = float(lag) if lag is not None else None
log.info("已加载状态: last_order_id=%s 去重缓存=%d 条",
state.last_order_id, len(state.pushed_ids))
except (OSError, ValueError, KeyError, TypeError) as exc:
@@ -65,6 +68,7 @@ class State:
"cookie_valid": self.cookie_valid,
"gap_since_ts": self.gap_since_ts,
"total_pushed": self.total_pushed,
"last_detect_lag_sec": self.last_detect_lag_sec,
}
def save(self, path: Optional[Path] = None) -> None: