fix(hub): prevent monitor board stuck on slow aggregate polling

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-03 21:47:11 +08:00
parent b5f66a0db2
commit 6a76993ca8
5 changed files with 96 additions and 16 deletions
+24 -2
View File
@@ -72,6 +72,7 @@ DIR = Path(__file__).resolve().parent
HUB_BUILD = "20260528-hub-market"
HUB_AGENT_TIMEOUT = float(os.getenv("HUB_AGENT_TIMEOUT", "8"))
HUB_FLASK_TIMEOUT = float(os.getenv("HUB_FLASK_TIMEOUT", "10"))
HUB_BOARD_TIMEOUT = float(os.getenv("HUB_BOARD_TIMEOUT", "45"))
_board_key_prices_raw = (os.getenv("HUB_BOARD_KEY_PRICES", "true") or "").strip().lower()
HUB_BOARD_KEY_PRICES = _board_key_prices_raw in ("1", "true", "yes", "on")
@@ -748,8 +749,7 @@ async def _assemble_board_row(
}
@app.get("/api/monitor/board")
async def api_monitor_board():
async def _build_monitor_board_payload() -> dict:
exchanges = enabled_exchanges()
async with httpx.AsyncClient() as client:
agent_rows = await asyncio.gather(
@@ -767,6 +767,28 @@ async def api_monitor_board():
}
@app.get("/api/monitor/board")
async def api_monitor_board():
try:
return await asyncio.wait_for(_build_monitor_board_payload(), timeout=HUB_BOARD_TIMEOUT)
except asyncio.TimeoutError:
return JSONResponse(
{
"ok": False,
"rows": [],
"error": "board_timeout",
"msg": (
f"监控聚合超过 {int(HUB_BOARD_TIMEOUT)} 秒。"
"请检查子代理/Flask,或设 HUB_BOARD_KEY_PRICES=false、缩短 HUB_FLASK_TIMEOUT"
),
"updated_at": __import__("datetime").datetime.now().isoformat(
timespec="seconds"
),
},
status_code=504,
)
def _require_hub_logged_in(request: Request) -> None:
if password_required() and not validate_session_token(request.cookies.get(SESSION_COOKIE)):
raise HTTPException(status_code=401, detail="未登录中控")