diff --git a/README.md b/README.md index 4591f9c..e8d2e44 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ 服务器 Docker ┌──────────────────────────────┐ fengxiang-monitor │ admin.py :8089 /healthz │ │ monitor.py 主循环 │ - │ ├─ client.py → 丰享订单 API │ ← 5~10s 轮询 + │ ├─ client.py → 丰享订单 API │ ← 营业 2s / 出单后 1.5s 轮询 │ ├─ store.py 状态去重 │ │ └─ notify.py → 企业微信 │ └──────────────────────────────┘ @@ -152,7 +152,12 @@ docker compose up -d --build # 改代码后 | 配置 | 默认 | 说明 | |------|------|------| -| `polling.interval_min/max` | 5 / 10 | 轮询间隔(秒,随机取值) | +| `polling.business_hours` | 9-21 | 营业时段 | +| `polling.interval_business` | 2 | 营业时段轮询间隔(秒) | +| `polling.interval_idle` | 8 | 非营业时段间隔(秒) | +| `polling.interval_burst` | 1.5 | 刚出单后的追击间隔(秒) | +| `polling.burst_window_sec` | 60 | 追击窗口(出单常成串) | +| `polling.interval_min/max` | 5 / 10 | 旧版随机间隔,缺 `interval_business` 时兜底 | | `polling.peak_page_size` | 20 | 高峰时段每次拉取条数 | | `polling.idle_page_size` | 5 | 闲时拉取条数 | | `polling.night_start/end` | 21:00 / 07:40 | 夜间暂停区间 | diff --git a/config.example.yaml b/config.example.yaml index 63a904d..a91c264 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -26,6 +26,11 @@ polling: peak_hours: [[11, 13], [17, 19]] # 高峰时段(左闭右开) interval_min: 5 interval_max: 10 + business_hours: [[9, 21]] # 营业时段(用 interval_business) + interval_business: 2 # 营业时段轮询间隔(秒):实测接口 ~300ms、2.5 次/秒无限流 + interval_idle: 8 # 非营业时段轮询间隔(秒) + interval_burst: 1.5 # 刚出单后的追击间隔(秒) + burst_window_sec: 60 # 追击窗口:出单常成串,窗口内加密轮询 peak_page_size: 20 idle_page_size: 5 night_start: "21:00" # 夜间暂停起点 diff --git a/fengxiang/config.py b/fengxiang/config.py index 79cf46b..3ca46c6 100644 --- a/fengxiang/config.py +++ b/fengxiang/config.py @@ -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"]), diff --git a/fengxiang/monitor.py b/fengxiang/monitor.py index a9e2004..40105cc 100644 --- a/fengxiang/monitor.py +++ b/fengxiang/monitor.py @@ -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: """可中断等待。""" diff --git a/fengxiang/store.py b/fengxiang/store.py index 3e74039..1f7fbde 100644 --- a/fengxiang/store.py +++ b/fengxiang/store.py @@ -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: diff --git a/sniff_console.py b/sniff_console.py new file mode 100644 index 0000000..95bf7aa --- /dev/null +++ b/sniff_console.py @@ -0,0 +1,63 @@ +"""探测丰享商家后台的数据获取方式:WebSocket / 长轮询 / 定时轮询。""" +from __future__ import annotations + +import json +import time +from collections import Counter +from pathlib import Path + +from playwright.sync_api import sync_playwright + +COOKIES = json.loads(Path("data/cookies.json").read_text(encoding="utf-8")) + +reqs: list[str] = [] +ws_urls: list[str] = [] +order_hits: list[str] = [] + + +def main() -> None: + with sync_playwright() as p: + browser = p.chromium.launch(headless=True) + ctx = browser.new_context() + ctx.add_cookies( + [{"name": k, "value": v, "domain": ".szfx.com", "path": "/"} for k, v in COOKIES.items()] + ) + page = ctx.new_page() + + page.on("websocket", lambda ws: ws_urls.append(ws.url)) + page.on("request", lambda r: reqs.append(r.url)) + + try: + page.goto("https://fs.szfx.com/MMS", wait_until="domcontentloaded", timeout=45000) + except Exception as exc: # noqa: BLE001 + print("打开 MMS 失败:", exc) + + # 停留在订单页,观察 60 秒,看是否有周期请求或 WebSocket 推消息 + for _ in range(6): + page.wait_for_timeout(10000) + print(f" +{(len(reqs))} 请求累计 {len(reqs)},WS {len(ws_urls)}") + + print("\n=== WebSocket ===") + for u in set(ws_urls): + print(" ", u[:160]) + + print("\n=== 订单/消息相关接口(去重)===") + seen = set() + for u in reqs: + if any(k in u.lower() for k in ("order", "notice", "message", "push", "socket", "notify")): + key = u.split("?")[0] + if key not in seen: + seen.add(key) + print(" ", key[:150]) + + print("\n=== 高频接口 TOP10(同一路径重复出现=轮询)===") + cnt = Counter(u.split("?")[0] for u in reqs) + for path, n in cnt.most_common(10): + print(f" {n:>3} 次 {path[:130]}") + + print(f"\n总计 {len(reqs)} 个请求,{len(ws_urls)} 个 WebSocket") + browser.close() + + +if __name__ == "__main__": + main() diff --git a/tests/test_core.py b/tests/test_core.py index a51982e..9be733e 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -10,6 +10,7 @@ import os import sys import tempfile import threading +import time import unittest import urllib.error import urllib.request @@ -310,5 +311,52 @@ class TestAdminAPI(BaseCase): self.assertEqual(ctx.exception.code, 400) +# --------------------------------------------------------------------------- 轮询节奏 +class TestPollingStrategy(BaseCase): + """分时段 + 出单后追击的节奏控制。""" + + def _mon_at(self, hour: int, minute: int = 0): + mon, *_ = self.build() + import datetime + + fixed = datetime.datetime(2026, 11, 18, hour, minute) + mon.now = lambda: fixed # 绕过真实时钟,稳定测试时段判断 + return mon + + def test_business_interval_is_fast(self): + mon = self._mon_at(12) + vals = [mon._next_interval() for _ in range(20)] + self.assertTrue(all(1.8 <= v <= 2.2 for v in vals), vals) + + def test_idle_interval_outside_business(self): + mon = self._mon_at(8) # 08:00:非营业、非夜间 + vals = [mon._next_interval() for _ in range(20)] + self.assertTrue(all(7.8 <= v <= 8.2 for v in vals), vals) + + def test_burst_overrides_business(self): + mon = self._mon_at(8) + mon._burst_until = time.time() + 60 + vals = [mon._next_interval() for _ in range(10)] + self.assertTrue(all(1.3 <= v <= 1.7 for v in vals), vals) + + def test_burst_armed_after_push(self): + mon, _, dispatcher, _ = self.build([make_order("A2"), make_order("A1")]) + mon.state.last_order_id = "A1" + mon._process([make_order("A2"), make_order("A1")], 10) + self.assertEqual(len(dispatcher.messages), 1) + self.assertGreater(mon._burst_until, time.time()) + + def test_detect_lag_from_pay_time(self): + mon, *_ = self.build() + lag = mon._detect_lag({"orderId": "A1", "payTime": time.time() - 3.2}) + self.assertTrue(3.0 <= lag <= 3.8, lag) + + def test_detect_lag_rejects_missing_and_stale(self): + mon, *_ = self.build() + self.assertIsNone(mon._detect_lag({"orderId": "x"})) + self.assertIsNone(mon._detect_lag({"payTime": time.time() - 90000})) # 超 24h + self.assertIsNone(mon._detect_lag({"payTime": "not-a-number"})) + + if __name__ == "__main__": unittest.main(verbosity=2)