cob update fix
This commit is contained in:
@ -60,6 +60,8 @@ class COBWebSocketStatus:
|
||||
class EnhancedCOBWebSocket:
|
||||
"""Enhanced COB WebSocket with robust error handling and fallback"""
|
||||
|
||||
_instances = {} # Track instances by symbols to prevent duplicates
|
||||
|
||||
def __init__(self, symbols: List[str] = None, dashboard_callback: Callable = None):
|
||||
"""
|
||||
Initialize Enhanced COB WebSocket
|
||||
@ -71,6 +73,18 @@ class EnhancedCOBWebSocket:
|
||||
self.symbols = symbols or ['BTC/USDT', 'ETH/USDT']
|
||||
self.dashboard_callback = dashboard_callback
|
||||
|
||||
# Check for existing instances to prevent duplicate connections
|
||||
symbols_key = tuple(sorted(self.symbols))
|
||||
if symbols_key in EnhancedCOBWebSocket._instances:
|
||||
logger.warning(f"EnhancedCOBWebSocket already exists for symbols {self.symbols} - reusing existing instance")
|
||||
existing = EnhancedCOBWebSocket._instances[symbols_key]
|
||||
# Copy existing instance data
|
||||
self.__dict__.update(existing.__dict__)
|
||||
return
|
||||
|
||||
# Register this instance
|
||||
EnhancedCOBWebSocket._instances[symbols_key] = self
|
||||
|
||||
# Connection status tracking
|
||||
self.status: Dict[str, COBWebSocketStatus] = {
|
||||
symbol: COBWebSocketStatus() for symbol in self.symbols
|
||||
@ -83,6 +97,10 @@ class EnhancedCOBWebSocket:
|
||||
# Latest data cache
|
||||
self.latest_cob_data: Dict[str, Dict] = {}
|
||||
|
||||
# Rate limiting for message processing
|
||||
self.last_message_time: Dict[str, datetime] = {}
|
||||
self.min_message_interval = 0.1 # Minimum 100ms between messages per symbol
|
||||
|
||||
# WebSocket connections
|
||||
self.websocket_tasks: Dict[str, asyncio.Task] = {}
|
||||
|
||||
@ -93,7 +111,7 @@ class EnhancedCOBWebSocket:
|
||||
|
||||
# Configuration
|
||||
self.max_depth = 1000 # Maximum depth for order book
|
||||
self.update_speed = '100ms' # Binance update speed
|
||||
self.update_speed = '1000ms' # Binance update speed - reduced for stability
|
||||
|
||||
logger.info(f"Enhanced COB WebSocket initialized for symbols: {self.symbols}")
|
||||
if not WEBSOCKETS_AVAILABLE:
|
||||
@ -149,6 +167,11 @@ class EnhancedCOBWebSocket:
|
||||
if self.rest_session:
|
||||
await self.rest_session.close()
|
||||
|
||||
# Remove from instances registry
|
||||
symbols_key = tuple(sorted(self.symbols))
|
||||
if symbols_key in EnhancedCOBWebSocket._instances:
|
||||
del EnhancedCOBWebSocket._instances[symbols_key]
|
||||
|
||||
logger.info("Enhanced COB WebSocket system stopped")
|
||||
|
||||
async def _init_rest_session(self):
|
||||
@ -321,7 +344,7 @@ class EnhancedCOBWebSocket:
|
||||
async def _websocket_connection_loop(self, symbol: str):
|
||||
"""Main WebSocket connection loop with reconnection logic
|
||||
|
||||
Uses depth@100ms for fastest updates with maximum depth.
|
||||
Uses depth@1000ms for stable updates with maximum depth.
|
||||
"""
|
||||
status = self.status[symbol]
|
||||
|
||||
@ -330,9 +353,9 @@ class EnhancedCOBWebSocket:
|
||||
logger.info(f"Attempting WebSocket connection for {symbol} (attempt {status.connection_attempts + 1})")
|
||||
status.connection_attempts += 1
|
||||
|
||||
# Create WebSocket URL with maximum depth - use depth@100ms for fastest updates
|
||||
# Create WebSocket URL with reasonable update rate - use depth@1000ms for stable updates
|
||||
ws_symbol = symbol.replace('/', '').lower() # BTCUSDT, ETHUSDT
|
||||
ws_url = f"wss://stream.binance.com:9443/ws/{ws_symbol}@depth@100ms"
|
||||
ws_url = f"wss://stream.binance.com:9443/ws/{ws_symbol}@depth@1000ms"
|
||||
|
||||
logger.info(f"Connecting to: {ws_url}")
|
||||
|
||||
@ -352,10 +375,19 @@ class EnhancedCOBWebSocket:
|
||||
# Message receiving loop
|
||||
async for message in websocket:
|
||||
try:
|
||||
# Rate limiting: skip messages that come too frequently
|
||||
now = datetime.now()
|
||||
if symbol in self.last_message_time:
|
||||
time_since_last = (now - self.last_message_time[symbol]).total_seconds()
|
||||
if time_since_last < self.min_message_interval:
|
||||
continue # Skip this message
|
||||
|
||||
self.last_message_time[symbol] = now
|
||||
|
||||
data = json.loads(message)
|
||||
await self._process_websocket_message(symbol, data)
|
||||
|
||||
status.last_message_time = datetime.now()
|
||||
status.last_message_time = now
|
||||
status.messages_received += 1
|
||||
|
||||
except json.JSONDecodeError as e:
|
||||
|
Reference in New Issue
Block a user