Files
Jyotisha/tests/test_api_rate_limit_2274.py
T
ClaudeandClaude Opus 5.5 73a44dba38
Independent Staging Quality Gate / validate-frontend (push) Canceled after 0s
Independent Staging Quality Gate / publish (push) Canceled after 0s
Independent Staging Quality Gate / validate-python (push) Canceled after 22s
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)
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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DbBXoyn7Hj3LCW3BUs5bZ8
2026-11-24 00:26:21 +08:00

36 lines
1.3 KiB
Python

"""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