fix(hub): restore pre-WS quote path and relieve Gate/account contention

Two audit rounds after WS rollback: lighten hub options snapshot, cache hub balances without extra fetch_balance, soft-poll single-flight, and document fixes in R1/R2 reports.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-11 12:30:27 +08:00
parent d592632834
commit bab42b1b53
12 changed files with 209 additions and 39 deletions
+18 -5
View File
@@ -467,6 +467,8 @@ from lib.exchange.gate_ccxt_lib import gate_ccxt_class
# Gate.io USDT 永续(swap)
exchange = gate_ccxt_class()({
"enableRateLimit": True,
# 避免关键位监控/账户拉取无限挂起拖垮中控
"timeout": int(os.getenv("GATE_CCXT_TIMEOUT_MS", "8000")),
"options": {
"defaultType": "swap",
"defaultMarginMode": _GATE_DEFAULT_MARGIN_MODE,
@@ -4611,14 +4613,25 @@ def _finalize_key_monitor_one_shot(conn, row, last_msg, close_reason):
conn.execute("DELETE FROM key_monitors WHERE id=?", (row["id"],))
_RS_BAR_CACHE: dict[str, dict] = {}
_RS_BAR_CACHE_TTL_SEC = float(os.getenv("GATE_RS_BAR_CACHE_SEC", "45"))
def _fetch_last_closed_bar(symbol):
"""最近一根闭合 K:[ts, o, h, l, c, v] 或 None."""
"""最近一根闭合 K:[ts, o, h, l, c, v] 或 None.短缓存减轻关键位监控打爆 ccxt."""
ex_sym = normalize_exchange_symbol(symbol)
now = time.time()
cached = _RS_BAR_CACHE.get(ex_sym)
if cached and now - float(cached.get("updated_at") or 0) < _RS_BAR_CACHE_TTL_SEC:
return cached.get("bar")
bars = exchange.fetch_ohlcv(ex_sym, timeframe=KLINE_TIMEFRAME, limit=5) or []
if len(bars) < 2:
_RS_BAR_CACHE[ex_sym] = {"updated_at": now, "bar": None}
return None
closed = bars[:-1]
return closed[-1] if closed else None
bar = closed[-1] if closed else None
_RS_BAR_CACHE[ex_sym] = {"updated_at": now, "bar": bar}
return bar
def _key_rs_gate_preview(symbol, upper, lower):
@@ -9893,14 +9906,14 @@ def _hub_meta_bundle():
def _hub_account_bundle():
funding_capital, trading_capital = get_exchange_capitals(force=True)
# 中控看板高频拉取:仅走余额缓存;不再额外 fetch_balance(会与关键位监控争用 ccxt)
funding_capital, trading_capital = get_exchange_capitals(force=False)
funding_usdt = round(funding_capital, 2) if funding_capital is not None else None
trading_usdt = round(trading_capital, 2) if trading_capital is not None else None
available = get_available_trading_usdt()
return {
"funding_usdt": funding_usdt,
"trading_usdt": trading_usdt,
"available_trading_usdt": round(available, 2) if available is not None else None,
"available_trading_usdt": trading_usdt,
"trading_day": get_trading_day(app_now()),
}