fix(settings): 端点测速在清单缓存未预热时 500

_endpoints_cache 初值是 {"ts": 0.0, "data": None}, "data" 键存在, 所以
dict.get("data", {}) 的默认值不生效, 返回的是 None。进程启动后到第一次
GET /api/settings/endpoints 之前, POST /api/settings/test_endpoint 不带
rounds 会在 None.get("testRounds", 5) 上抛 AttributeError → HTTP 500。

前端 endpoints 查询有 5 分钟 staleTime, 后端重启后用户再点测速正好落在
这个窗口: 前端不会重新拉清单, 后端缓存却已清空。

改为先 `or {}` 兜底再取 testRounds, 保持原有默认 5 轮语义。
This commit is contained in:
kevin9327
2026-09-09 07:12:54 +09:00
parent 9a4bdcd07d
commit e349f2ba9b
2 changed files with 51 additions and 1 deletions
+3 -1
View File
@@ -1666,7 +1666,9 @@ async def test_endpoint(req: TestEndpointIn) -> dict:
import statistics
base = req.url.rstrip("/")
rounds = max(1, min(10, req.rounds or _endpoints_cache.get("data", {}).get("testRounds", 5)))
# 缓存初值的 "data" 是 None(键存在, get 的默认值不生效), 端点清单未预热时要兜底
manifest = _endpoints_cache.get("data") or {}
rounds = max(1, min(10, req.rounds or manifest.get("testRounds", 5)))
health_url = base + "/health"
latencies: list[float] = []