From 03218dce047650e0942541b487c8f38a628eb443 Mon Sep 17 00:00:00 2001 From: root Date: Thu, 26 Feb 2026 13:57:39 +0000 Subject: [PATCH] fix: split rate/stats refresh intervals, rates 2s stats 2min --- backend/main.py | 7 ++++--- frontend/app/page.tsx | 24 +++++++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/backend/main.py b/backend/main.py index 005b21f..a5acdc9 100644 --- a/backend/main.py +++ b/backend/main.py @@ -15,6 +15,7 @@ app.add_middleware( BINANCE_FAPI = "https://fapi.binance.com/fapi/v1" SYMBOLS = ["BTCUSDT", "ETHUSDT"] +HEADERS = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"} # 简单内存缓存(history/stats 60秒,rates 3秒) _cache: dict = {} @@ -38,7 +39,7 @@ async def health(): async def get_rates(): cached = get_cache("rates", 3) if cached: return cached - async with httpx.AsyncClient(timeout=10) as client: + async with httpx.AsyncClient(timeout=10, headers=HEADERS) as client: tasks = [client.get(f"{BINANCE_FAPI}/premiumIndex", params={"symbol": s}) for s in SYMBOLS] responses = await asyncio.gather(*tasks) result = {} @@ -65,7 +66,7 @@ async def get_history(): if cached: return cached end_time = int(datetime.utcnow().timestamp() * 1000) start_time = int((datetime.utcnow() - timedelta(days=7)).timestamp() * 1000) - async with httpx.AsyncClient(timeout=15) as client: + async with httpx.AsyncClient(timeout=15, headers=HEADERS) as client: tasks = [ client.get(f"{BINANCE_FAPI}/fundingRate", params={"symbol": s, "startTime": start_time, "endTime": end_time, "limit": 1000}) @@ -92,7 +93,7 @@ async def get_stats(): if cached: return cached end_time = int(datetime.utcnow().timestamp() * 1000) start_time = int((datetime.utcnow() - timedelta(days=7)).timestamp() * 1000) - async with httpx.AsyncClient(timeout=15) as client: + async with httpx.AsyncClient(timeout=15, headers=HEADERS) as client: tasks = [ client.get(f"{BINANCE_FAPI}/fundingRate", params={"symbol": s, "startTime": start_time, "endTime": end_time, "limit": 1000}) diff --git a/frontend/app/page.tsx b/frontend/app/page.tsx index a30684f..e3342d1 100644 --- a/frontend/app/page.tsx +++ b/frontend/app/page.tsx @@ -13,12 +13,10 @@ export default function Dashboard() { const [status, setStatus] = useState<"loading" | "running" | "error">("loading"); const [lastUpdate, setLastUpdate] = useState(""); - const fetchAll = useCallback(async () => { + const fetchRates = useCallback(async () => { try { - const [r, s, h] = await Promise.all([api.rates(), api.stats(), api.history()]); + const r = await api.rates(); setRates(r); - setStats(s); - setHistory(h); setStatus("running"); setLastUpdate(new Date().toLocaleTimeString("zh-CN")); } catch { @@ -26,11 +24,23 @@ export default function Dashboard() { } }, []); + const fetchAll = useCallback(async () => { + try { + const [s, h] = await Promise.all([api.stats(), api.history()]); + setStats(s); + setHistory(h); + } catch { + // stats/history 失败不影响主界面 + } + }, []); + useEffect(() => { + fetchRates(); fetchAll(); - const interval = setInterval(fetchAll, 2_000); - return () => clearInterval(interval); - }, [fetchAll]); + const rateInterval = setInterval(fetchRates, 2_000); // 价格 2秒刷新 + const slowInterval = setInterval(fetchAll, 120_000); // 统计 2分钟刷新 + return () => { clearInterval(rateInterval); clearInterval(slowInterval); }; + }, [fetchRates, fetchAll]); return (