From 73a44dba38c995cc2201244b0d5df2ab7ed96891 Mon Sep 17 00:00:00 2001 From: Claude Date: Mon, 23 Nov 2026 23:42:46 +0800 Subject: [PATCH] fix(prod): chart-choice waits 180 s for an adaptive scan; the private API's default limit is 3000/min, not one 120/min bucket for the whole site (BUG-2274) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Production 2-core host: adaptive packet scans measured 21-163 s cold, the web side aborted at 8 s (BrokenPipe, 「排盘服务暂时没有响应」). The API's per-IP limiter saw only the web container, so every user shared 120 calls a minute (jyotish_api_429 on launch night). Frozen-file change limited to the limiter default, owner-approved for production blockers. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8 --- docs/BUG_HISTORY.md | 22 ++++++++++++ frontend/src/lib/chart-choice/server.ts | 7 ++-- .../chart-choice-engine-timeout-2274.test.ts | 14 ++++++++ scripts/jyotish_api_server.py | 10 ++++-- tests/test_api_rate_limit_2274.py | 35 +++++++++++++++++++ 5 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 frontend/tests/chart-choice-engine-timeout-2274.test.ts create mode 100644 tests/test_api_rate_limit_2274.py diff --git a/docs/BUG_HISTORY.md b/docs/BUG_HISTORY.md index 6cb938db8..44f0a2987 100644 --- a/docs/BUG_HISTORY.md +++ b/docs/BUG_HISTORY.md @@ -30648,3 +30648,25 @@ - 防复发:键盘开关不能只看 `visualViewport`;凡是会主动改窗口滚动的代码,焦点在文字输入框时都不得动。 - 关联记录:BUG-920(复位逻辑的来源;本条不是它的复发,而是它在微信内置浏览器里的副作用)、BUG-1916、BUG-2259。 - 修复版本:分支 `claude/mobile-composer-keyboard-2277`(待推 staging)。 + +## BUG-2274 | 生产上线当晚:生时纠正报「排盘服务暂时没有响应」,排盘接口出现 429 + +- 状态:fixed-pending-verify(待部署到生产后在生时纠正页复看) +- 首次发现 / 最近更新:2026-11-23(负责人在生产 `/rectification` 截图)/ 2026-11-23 +- 现象: + - 生时纠正页(`adaptive: true`)约 6–8 秒后报「排盘服务暂时没有响应,请稍后再试。」;稍后重试又能打开。 + - 网页日志有 `ephemeris_events_unavailable Error: jyotish_api_429`;排盘服务日志有一次 `varga-segments` 429。 +- 触发条件:生产主机 2 核;自适应扫描冷启动;多个页面同时调用排盘服务。 +- 根因: + - `frontend/src/lib/chart-choice/server.ts` 调排盘服务的上限是 8 秒(宽范围 20 秒)。自适应扫描冷启动在本机实测 21 秒到 163 秒,网页先断开;排盘服务算完写回时 `BrokenPipeError`,日志里同一请求先记 200 再记 500。 + - `scripts/jyotish_api_server.py` 的限流默认 120 次/分钟、按来源 IP 计数。排盘服务只在 Docker 内网给网页容器用,所有用户共用一个桶;一张星盘页约 10 次调用,健康检查每分钟约 4–5 次,几个人同时在线就会 429。 +- 修复: + - 生时纠正调排盘服务的两个上限都改为 180 秒;窄范围照旧零点几秒返回。等待时页面沿用原有的「正在计算…」。 + - 限流默认改为 3000 次/分钟,环境变量 `JYOTISH_API_RATE_LIMIT_PER_MINUTE` 仍可覆盖;冻结文件只改限流这几行(负责人批准修生产阻断问题)。 +- 验证: + - 本机计时(公开 AA 盘,Raman/平交点):±15、±30 分钟的 packet/decide 0.03–0.06 秒;±60 分钟自适应 packet 21.2 秒、decide 0.15 秒;另一张公开盘的两小时自适应 packet 163.3 秒和 3.0 秒(另一时段)。 + - 新增 `tests/test_api_rate_limit_2274.py`(3 条)和 `frontend/tests/chart-choice-engine-timeout-2274.test.ts`(1 条)。 +- 防复发:内网服务的限流不能按来源 IP 把全站当成一个用户;网页等排盘服务的上限要按生产主机上的实测冷启动时间定,不按开发机。 +- 未做:自适应扫描本身 20–160 秒偏慢,需要另行优化(缓存预热或缩小首轮扫描);其他网页→排盘调用的上限(星盘 10 秒、出生天空 10 秒、上升预检 3 秒等)生产日志里没有超时记录,这次不改。 +- 关联记录:BUG-016(首轮扫描超时打成 503)、BUG-2064(自选范围扫描)。 +- 修复版本:分支 `claude/prod-engine-limits-2274`(本地提交,待推送)。 diff --git a/frontend/src/lib/chart-choice/server.ts b/frontend/src/lib/chart-choice/server.ts index f3708a558..f0d6df276 100644 --- a/frontend/src/lib/chart-choice/server.ts +++ b/frontend/src/lib/chart-choice/server.ts @@ -35,8 +35,11 @@ import { declaredNeedsRange } from "./declared.ts"; import { statedBirthSecond } from "../server-owned-birth-profile.ts"; const apiBase = () => process.env.JYOTISH_API_BASE?.trim() || "http://127.0.0.1:5200"; -const ENGINE_TIMEOUT_MS = 8000; -const RESTORE_WIDE_RANGE_TIMEOUT_MS = 20_000; +// BUG-2274 原值 8000 / 20_000 毫秒;新值 180_000 / 180_000;原因:生产 2 核主机上自适应扫描 +// (adaptive: true)冷启动实测 21 s 到 163 s(本机 8 核同样量级),8 s 时网页先断开,引擎算完写回时 +// BrokenPipe,用户看到「排盘服务暂时没有响应」。窄范围照旧零点几秒返回,加长上限不影响它们。 +const ENGINE_TIMEOUT_MS = 180_000; +const RESTORE_WIDE_RANGE_TIMEOUT_MS = 180_000; const ROUNDS_VERSION = "chart-choice-v2"; const MAX_RECORD_SPAN_MINUTES = Math.max(...RECORD_RANGE_CHOICES); /** The longest range written back to a profile (the database allows 720 min on either side). */ diff --git a/frontend/tests/chart-choice-engine-timeout-2274.test.ts b/frontend/tests/chart-choice-engine-timeout-2274.test.ts new file mode 100644 index 000000000..d5557a272 --- /dev/null +++ b/frontend/tests/chart-choice-engine-timeout-2274.test.ts @@ -0,0 +1,14 @@ +import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; +import test from "node:test"; + +const source = readFileSync(new URL("../src/lib/chart-choice/server.ts", import.meta.url), "utf8"); + +test("BUG-2274: chart-choice engine calls wait long enough for an adaptive scan on a 2-core host", () => { + // 原值 8000 / 20_000;新值 180_000;原因:自适应扫描冷启动实测 21–163 s,8 s 时网页先断开。 + const normal = Number(/const ENGINE_TIMEOUT_MS = ([\d_]+);/u.exec(source)?.[1]?.replaceAll("_", "")); + const wide = Number(/const RESTORE_WIDE_RANGE_TIMEOUT_MS = ([\d_]+);/u.exec(source)?.[1]?.replaceAll("_", "")); + assert.ok(normal >= 180_000, `normal timeout ${normal}`); + assert.ok(wide >= normal, `wide timeout ${wide}`); + assert.match(source, /signal: AbortSignal\.timeout\(timeoutMs\)/u); +}); diff --git a/scripts/jyotish_api_server.py b/scripts/jyotish_api_server.py index f6e340f0a..43f9a7bfa 100644 --- a/scripts/jyotish_api_server.py +++ b/scripts/jyotish_api_server.py @@ -472,12 +472,18 @@ def build_evidence_packet_view(job_record: dict | None) -> dict: } +# BUG-2274 原值:默认 120 次/分钟;新值:默认 3000 次/分钟(环境变量仍可覆盖); +# 原因:这个 API 只在 Docker 内网里给网页容器调用,按来源 IP 计数时所有用户共用一个桶, +# 一张星盘页约 10 次调用,生产上线当晚就出现 jyotish_api_429。上限仍在,只是不再把全站当一个人。 +_DEFAULT_RATE_LIMIT_PER_MINUTE = 3000 + + def _rate_limit_per_minute() -> int: - raw = str(os.environ.get('JYOTISH_API_RATE_LIMIT_PER_MINUTE', '120')).strip() + raw = str(os.environ.get('JYOTISH_API_RATE_LIMIT_PER_MINUTE', str(_DEFAULT_RATE_LIMIT_PER_MINUTE))).strip() try: return max(int(raw), 0) except ValueError: - return 120 + return _DEFAULT_RATE_LIMIT_PER_MINUTE def enforce_rate_limit(client_id: str, *, now: float | None = None) -> None: diff --git a/tests/test_api_rate_limit_2274.py b/tests/test_api_rate_limit_2274.py new file mode 100644 index 000000000..144a5ec73 --- /dev/null +++ b/tests/test_api_rate_limit_2274.py @@ -0,0 +1,35 @@ +"""BUG-2274: the private API's limiter must not treat the whole site as one user.""" +import importlib + +import pytest + + +@pytest.fixture() +def api(monkeypatch): + monkeypatch.delenv("JYOTISH_API_RATE_LIMIT_PER_MINUTE", raising=False) + module = importlib.import_module("scripts.jyotish_api_server") + module._RATE_LIMIT_BUCKETS.clear() + yield module + module._RATE_LIMIT_BUCKETS.clear() + + +def test_default_allows_a_busy_minute_from_the_single_web_client(api): + # 原值:默认 120 次/分钟,网页容器一个来源 IP 就是全站;新值 3000;原因:生产上线当晚 jyotish_api_429。 + assert api._rate_limit_per_minute() == 3000 + for second in range(1500): + api.enforce_rate_limit("172.19.0.3", now=second / 100) + + +def test_the_limit_is_still_bounded(api): + for _ in range(3000): + api.enforce_rate_limit("172.19.0.3", now=1.0) + with pytest.raises(api.RateLimited): + api.enforce_rate_limit("172.19.0.3", now=2.0) + api.enforce_rate_limit("172.19.0.3", now=61.5) + + +def test_environment_still_overrides(api, monkeypatch): + monkeypatch.setenv("JYOTISH_API_RATE_LIMIT_PER_MINUTE", "5") + assert api._rate_limit_per_minute() == 5 + monkeypatch.setenv("JYOTISH_API_RATE_LIMIT_PER_MINUTE", "not-a-number") + assert api._rate_limit_per_minute() == 3000