LUCIT-Systems-and-Development / unicorn-binance-websocket-api

A Python SDK by LUCIT to use the Binance Websocket API`s (com+testnet, com-margin+testnet, com-isolated_margin+testnet, com-futures+testnet, com-coin_futures, us, tr, dex/chain+testnet) in a simple, fast, flexible, robust and fully-featured way.
https://unicorn-binance-websocket-api.docs.lucit.tech/
Other
685 stars 165 forks source link

How does the websocket manager handle "Service under Maintenance" exception? #151

Closed ashwinvenkatram closed 3 years ago

ashwinvenkatram commented 3 years ago

Hello,

I am using this API to interface with Binance USDT-M Futures.

Earlier today I encountered a "Service under Maintenance" exception when Binance triggered their scheduled system update. I came across a comment in the documentation which states this is being handled internally. I'm curious to know more about how this exception is being handled and the websocket behaviour.

For some context, my program is running two processes in parallel. First, an instance of the websocket from this API which places klines data on a queue once a specified interval is complete. A 2nd process then reads this data to perform post processing.

My questions are:

  1. What is the status of the websocket during the "Service under Maintenance"? Is it active? Or will it close and re-open upon server side recovery?
  2. In case the websocket is active, should I expect to see data streaming in from Binance although the service is down?
  3. Could someone point me to the location where this exception is being handled within the API?

Thanks very much.

oliver-zehentleitner commented 3 years ago

Hello!

This depends on the maintenance task of Binance. In most cases I just receive nothing but stay connected. After a while trading continues and also the stream does. Its also possible that the connection to API endpoints can not get established till maintenance is over - in such a case you are disconnected.

There is no exception for maintenance windows, only for a non repairable webstream (wrong api key or something else) and this exception is only thrown if you set throw_exception_if_unrepairable=True

If you want to get a notification if the status of the websocket connection changes you can use the stream_signal_buffer

Exception handling is in https://github.com/oliver-zehentleitner/unicorn-binance-websocket-api/blob/master/unicorn_binance_websocket_api/unicorn_binance_websocket_api_socket.py

Best regards!

ashwinvenkatram commented 3 years ago

Hi Oliver,

Thanks for sharing the expected behaviour of the websocket. It's interesting that the behaviour can change depending on the maintenance task of Binance! I was under the impression any time the "Service down for maintenance" exception is encountered, the behaviour is always the same.

In my case, I was running a test of my program and computes indicator data for every minute of OHLCV data. From my logs, I found that the indicator data was being computed although the service was down for maintenance. The values converged and remained steady during the maintenance period. This seems to indicate that the websocket was actually alive, and Binance in this case, was sending me the last frozen OHLCV data when maintenance was triggered.

The notification of status change on the websocket is definitely helpful.

Thank you!

oliver-zehentleitner commented 3 years ago

Your welcome!