Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

websocket connection resets every two minutes #471

Open
jepessen opened this issue Aug 1, 2022 · 1 comment
Open

websocket connection resets every two minutes #471

jepessen opened this issue Aug 1, 2022 · 1 comment

Comments

@jepessen
Copy link

jepessen commented Aug 1, 2022

I've the following class for retrieving data from websocket and convert them in a format usable by my program:

from curses import cbreak
import cbpro
import logging
import schedule
import time
import sys

# Setting log
log_formatter = logging.Formatter("%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s]  %(message)s")
root_logger = logging.getLogger()
file_handler = logging.FileHandler("cbpro.log")
file_handler.setFormatter(log_formatter)
root_logger.addHandler(file_handler)

console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(log_formatter)
root_logger.addHandler(console_handler)
root_logger.setLevel(logging.DEBUG)

class CBProFeed(cbpro.WebsocketClient):
  def __init__(self):
    cbpro.WebsocketClient.__init__(self)
    self.public_client = cbpro.PublicClient()
    self.main_currency = 'EUR'
    self.crypto_currencies = ['AUCTION']
    self.products = ['AUCTION-EUR']

  def on_open(self):
    self.url = "wss://ws-feed.pro.coinbase.com/"
    self.channels=["ticker"]


  def on_message(self, msg):
    if 'price' in msg and 'type' in msg and msg['type'] == 'ticker':
      product_id = msg['product_id']
      price = float(msg['price'])
      volume = float(msg['volume_24h'])
      logging.info("Received " + product_id + ", Price is " + str(price) + ". Volume 24H is " + str(volume));

  def on_close(self):
    logging.info("Websocket connection closed")

  def run_forever(self):
    logging.info("Starting feed")
    schedule.every().minute.at(':00').do(self._handle_candle_callback)
    self.start()
    while True:
      schedule.run_pending()
      time.sleep(.1);
      if (self.error):
        logging.error("Connection closed. Error is " + str(self.error) + ". Restarting...")
        self.close()
        self.error = None
        self.start()

  def _handle_candle_callback(self):
    logging.debug("Minute processed")


feed = CBProFeed()
feed.run_forever()
logging.info("End of the script")

It works, except for the fact that the connection resets about every two minutes. This is the log when I run it:

...
2022-08-01 14:20:39,367 [MainThread  ] [INFO ]  Starting feed
2022-08-01 14:20:40,016 [Thread-1    ] [INFO ]  Received AUCTION-EUR, Price is 7.52. Volume 24H is 7511.306
2022-08-01 14:21:00,003 [MainThread  ] [DEBUG]  Running job Job(interval=1, unit=minutes, do=_handle_candle_callback, args=(), kwargs={})
2022-08-01 14:21:00,003 [MainThread  ] [DEBUG]  Minute processed
2022-08-01 14:22:00,023 [MainThread  ] [DEBUG]  Running job Job(interval=1, unit=minutes, do=_handle_candle_callback, args=(), kwargs={})
2022-08-01 14:22:00,023 [MainThread  ] [DEBUG]  Minute processed
Connection is already closed. - data: None
2022-08-01 14:22:20,089 [Thread-1    ] [INFO ]  Websocket connection closed
2022-08-01 14:22:20,147 [MainThread  ] [ERROR]  Connection closed. Error is Connection is already closed.. Restarting...
2022-08-01 14:22:20,829 [Thread-2    ] [INFO ]  Received AUCTION-EUR, Price is 7.52. Volume 24H is 7511.306
2022-08-01 14:22:34,593 [Thread-2    ] [INFO ]  Received AUCTION-EUR, Price is 7.53. Volume 24H is 7667.951
2022-08-01 14:22:41,850 [Thread-2    ] [INFO ]  Received AUCTION-EUR, Price is 7.53. Volume 24H is 7668.243
2022-08-01 14:22:46,204 [Thread-2    ] [INFO ]  Received AUCTION-EUR, Price is 7.54. Volume 24H is 7667.951
2022-08-01 14:22:46,247 [Thread-2    ] [INFO ]  Received AUCTION-EUR, Price is 7.55. Volume 24H is 7670.651
2022-08-01 14:22:46,798 [Thread-2    ] [INFO ]  Received AUCTION-EUR, Price is 7.56. Volume 24H is 7673.351
2022-08-01 14:22:46,996 [Thread-2    ] [INFO ]  Received AUCTION-EUR, Price is 7.57. Volume 24H is 7676.051
2022-08-01 14:23:00,014 [MainThread  ] [DEBUG]  Running job Job(interval=1, unit=minutes, do=_handle_candle_callback, args=(), kwargs={})
2022-08-01 14:23:00,015 [MainThread  ] [DEBUG]  Minute processed
2022-08-01 14:24:00,052 [MainThread  ] [DEBUG]  Running job Job(interval=1, unit=minutes, do=_handle_candle_callback, args=(), kwargs={})
2022-08-01 14:24:00,052 [MainThread  ] [DEBUG]  Minute processed
Connection is already closed. - data: None
2022-08-01 14:24:27,123 [Thread-2    ] [INFO ]  Websocket connection closed
2022-08-01 14:24:27,129 [MainThread  ] [ERROR]  Connection closed. Error is Connection is already closed.. Restarting...
2022-08-01 14:24:27,788 [Thread-3    ] [INFO ]  Received AUCTION-EUR, Price is 7.57. Volume 24H is 7679.043
...

That are logs that appear when on_close is called. What I'm doing wrong and how can solve the problem?

@jepessen
Copy link
Author

jepessen commented Aug 1, 2022

I've checked a bit and it seems to be related with AUCTION currency. If I try to retrieve data for example for BTC-EUR the connection is not closed. the script receives for AUCTION a lot less messages than BTC. My supposition is that if the server does not send messages for a specific amount of time (i.e. 100 seconds), the connection is automatically closed... Is there a way to maintain it opened?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@jepessen and others