bmoscon / cryptofeed

Cryptocurrency Exchange Websocket Data Feed Handler
Other
2.19k stars 679 forks source link

Do XX on websocket error? #738

Closed warproxxx closed 2 years ago

warproxxx commented 2 years ago

I am listening to bybit websocket and doing operations on trades

f = FeedHandler() f.add_feed(BYBIT, channels=[TRADES], symbols=['BTC-USD-PERP'], callbacks={TRADES: trade}, timeout=-1) f.run()

Whenever the live wss goes down, I get this expected trackback error.

WARNING : BYBIT.ws.1: encountered connection issue no close frame received or sent - reconnecting in 1.0 seconds... Traceback (most recent call last): File "/home/ubuntu/.local/lib/python3.8/site-packages/websockets/legacy/protocol.py", line 944, in transfer_data message = await self.read_message() File "/home/ubuntu/.local/lib/python3.8/site-packages/websockets/legacy/protocol.py", line 1013, in read_message frame = await self.read_data_frame(max_size=self.max_size) File "/home/ubuntu/.local/lib/python3.8/site-packages/websockets/legacy/protocol.py", line 1089, in read_data_frame frame = await self.read_frame(max_size) File "/home/ubuntu/.local/lib/python3.8/site-packages/websockets/legacy/protocol.py", line 1144, in read_frame frame = await Frame.read( File "/home/ubuntu/.local/lib/python3.8/site-packages/websockets/legacy/framing.py", line 70, in read data = await reader(2) File "/usr/lib/python3.8/asyncio/streams.py", line 723, in readexactly await self._wait_for_data('readexactly') File "/usr/lib/python3.8/asyncio/streams.py", line 517, in _wait_for_data await self._waiter ConnectionResetError: [Errno 104] Connection reset by peer

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "/home/ubuntu/.local/lib/python3.8/site-packages/cryptofeed/connection_handler.py", line 69, in _create_connection await self._handler(connection, self.handler) File "/home/ubuntu/.local/lib/python3.8/site-packages/cryptofeed/connection_handler.py", line 115, in _handler async for message in connection.read(): File "/home/ubuntu/.local/lib/python3.8/site-packages/cryptofeed/connection.py", line 320, in read async for data in self.conn: File "/home/ubuntu/.local/lib/python3.8/site-packages/websockets/legacy/protocol.py", line 481, in aiter yield await self.recv() File "/home/ubuntu/.local/lib/python3.8/site-packages/websockets/legacy/protocol.py", line 552, in recv await self.ensure_open() File "/home/ubuntu/.local/lib/python3.8/site-packages/websockets/legacy/protocol.py", line 920, in ensure_open raise self.connection_closed_exc() websockets.exceptions.ConnectionClosedError: no close frame received or sent`

Because of timeout=-1 the program continues to run as expected once the socket is back. But whenever there an error in wss (ie the above takes place) i want to call a function (to cancel all orders in bybit). How do i call a function on error?

bmoscon commented 2 years ago

you'd have to register an exception handler, here: https://github.com/bmoscon/cryptofeed/blob/master/cryptofeed/feedhandler.py#L122

also see https://github.com/bmoscon/cryptofeed/issues/506 where I posted an example of a custom exception handler