Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Sep 13, 2024
1 parent d262755 commit e017398
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions exchange/_bybit_ws.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async def _connect(self):
close_timeout=3,
)

await self._wait_for_ws()
await self._wait_for_ws(timeout=5)
await self._subscribe()

logger.info("WebSocket connection established.")
Expand Down Expand Up @@ -123,7 +123,7 @@ async def _subscribe(self):
async def _unsubscribe(self):
await self._send_message(self.UNSUBSCRIBE_OPERATION)

async def _send_message(self, operation):
async def _send_message(self, operation, timeout=5):
if (
not self.ws
or not self.ws.open
Expand All @@ -137,17 +137,24 @@ async def _send_message(self, operation):
}

try:
await asyncio.wait_for(self.ws.send(json.dumps(message)), timeout=5)
await asyncio.wait_for(self.ws.send(json.dumps(message)), timeout=timeout)

logger.info(f"{operation.capitalize()} to: {message}")
except asyncio.TimeoutError:
logger.error("Subscription request timed out")
except Exception as e:
logger.error(f"Failed to send {operation} message: {e}")

async def _wait_for_ws(self):
async def _wait_for_ws(self, timeout=10):
try:
await asyncio.wait_for(self._check_ws_open(), timeout=timeout)
except asyncio.TimeoutError:
logger.error("Timed out waiting for WebSocket to open.")
raise ConnectionError("WebSocket connection timeout.")

async def _check_ws_open(self):
while not self.ws or not self.ws.open:
await asyncio.sleep(1.0)
await asyncio.sleep(0.1)

def _is_valid_message(self, symbol, timeframe, data):
if self.TOPIC_KEY not in data:
Expand Down

0 comments on commit e017398

Please sign in to comment.