perf(aura-trade): adaptive WS streamer, 1.5s CDP bridge cache, live proxy fallback, and persistent /app/data bind mount

This commit is contained in:
Amirul Islam Redwan71
2026-11-23 12:37:33 +06:00
parent b899020d9f
commit 0485b87fe7
5 changed files with 302 additions and 137 deletions
+32 -15
View File
@@ -38,26 +38,43 @@ executor = OrderExecutor(connector=connector, sentinel=sentinel)
# Background Market Streamer Daemon
async def background_market_streamer():
"""Background loop streaming sub-second ticks & telemetry to connected WebSockets."""
logger.info("Starting Background Market Streamer Daemon...")
"""Adaptive background loop: sleeps cleanly when idle (0 WS clients), streams live ticks when connected."""
import time
logger.info("Starting Adaptive Background Market Streamer Daemon...")
last_news_check = 0.0
last_calendar_refresh = time.monotonic()
cached_blackout = (False, "Clear: No upcoming high-impact USD events within blackout window.")
while True:
try:
# 1. Fetch live tick
tick = connector.get_live_tick(settings.TARGET_SYMBOL)
# 2. Fetch live telemetry
telemetry = connector.get_telemetry()
# 3. Check news blackout
is_blackout, news_reason = news_filter.is_news_blackout_active()
now_mono = time.monotonic()
# Refresh ForexFactory economic calendar every 6 hours
if (now_mono - last_calendar_refresh) >= 21600.0:
await news_filter.fetch_calendar()
last_calendar_refresh = now_mono
# 4. Broadcast packet via WebSocket (UHF 20Hz Tick Stream)
import time
# Zero wasted bridge polling when no browser terminal is viewing WebSocket
if not ws_manager.active_connections:
await asyncio.sleep(2.0)
continue
# 1. Fetch live tick & telemetry in worker thread (shares 1.5s TTL cache inside connector)
tick = await asyncio.to_thread(connector.get_live_tick, settings.TARGET_SYMBOL)
telemetry = await asyncio.to_thread(connector.get_telemetry)
# 2. Check news blackout at most every 30s
if (now_mono - last_news_check) >= 30.0:
cached_blackout = news_filter.is_news_blackout_active()
last_news_check = now_mono
is_blackout, news_reason = cached_blackout
# 3. Broadcast packet via WebSocket (2Hz active stream)
now_sec = time.time()
packet = {
"type": "MARKET_UPDATE",
"symbol": settings.TARGET_SYMBOL,
"timestamp_us": int(time.time() * 1_000_000),
"timestamp_ms": int(time.time() * 1000),
"timestamp_us": int(now_sec * 1_000_000),
"timestamp_ms": int(now_sec * 1000),
"tick": tick,
"telemetry": telemetry,
"news_quarantine": {
@@ -74,7 +91,7 @@ async def background_market_streamer():
except Exception as e:
logger.error(f"Error in market streamer: {e}")
await asyncio.sleep(0.05) # UHF 50ms real-time broadcast interval (20 ticks/sec)
await asyncio.sleep(0.5) # 2Hz active broadcast interval when clients are connected
@asynccontextmanager
async def lifespan(app: FastAPI):