Crypto-toolbox / btfxwss

Bitfinex Websocket API Client written in Python3
MIT License
284 stars 125 forks source link

Reconnecting client #130

Closed pegazik80 closed 6 years ago

pegazik80 commented 6 years ago

I am not sure whether other people see the same issue but from time to time my client becomes stale and data stops arriving. I have tried two approaches to deal with this problem in the main loop: Approach A) - eventually resulted in an error "too many open files", unfortunately the log that contained exact exception has been deleted.

        if self.wss_client is None or self.wss_client.conn.connected.is_set():
            if self.WSS_CLIENT_VERSION == 1:
                self.wss_client = BtfxWss(url="wss://api.bitfinex.com/ws")  # v1
            else:
                self.wss_client = BtfxWss()  # v2

            self.wss_client.start()
            while not self.wss_client.conn.connected.is_set():
                time.sleep(1)
            self.logger.info("wss client connected")

Approach B) - There is no exception but after disconnection I end up in neverending loop printing ever second reset(): "Waiting for connection to be set up.." and not being able to pick it up again.

        if self.wss_client is None:
            if self.WSS_CLIENT_VERSION == 1:
                self.wss_client = BtfxWss(url="wss://api.bitfinex.com/ws")  # v1
            else:
                self.wss_client = BtfxWss()  # v2

            self.wss_client.start()
            while not self.wss_client.conn.connected.is_set():
                time.sleep(1)
            self.logger.info("wss client connected")

        if not self.wss_client.conn.connected.is_set():
            self.wss_client.reset()
            self.logger.info("wss client re-connected")

What is the correct way of dealing with client reconnect events?

Thanks for great work by the way :)

pegazik80 commented 6 years ago

I am sorry, the problem I reported was actually originating somewhere else. The code Approach A) is working well, I already saw few cases of spontaneous dis- and re-connections using this code.