HuobiRDCenter / huobi_Python

Python SDK for Huobi Spot API
https://huobiapi.github.io/docs/spot/v1/en
Apache License 2.0
682 stars 333 forks source link

Reconnecting in case of network shutdown: ERROR #47

Open mkvdv-l3tech opened 4 years ago

mkvdv-l3tech commented 4 years ago

Hello! Thank you for this sdk, it is helpful!

I am testing this sdk now, and there is a trouble with this snippet (this is from examples, like this file, but a little bit modified -- subscribe_price_depth_event instead of request_price_depth_event)

import logging

from huobi import SubscriptionClient
from huobi.exception.huobiapiexception import HuobiApiException
from huobi.model.pricedepthrequest import PriceDepthRequest, DepthStep

logger = logging.getLogger(__name__)
logger.setLevel(level=logging.INFO)
handler = logging.StreamHandler()
handler.setFormatter(
    logging.Formatter(
        '[%(asctime)s][%(name)s][%(levelname)s]: %(message)s'))
logger.addHandler(handler)

def callback(price_depth_event: PriceDepthRequest):
    """
    Just log the event
    """
    depth = price_depth_event.data

    bids = ', '.join(f"({entry.price}, {entry.amount})" for entry in depth.bids)
    asks = ', '.join(f"({entry.price}, {entry.amount})" for entry in depth.asks)

    logger.info(
        f"ts: {price_depth_event.timestamp}, ch: {price_depth_event.ch}, "
        f"bids={bids}, asks={asks}")

def error(e: HuobiApiException):
    logger.error(f"{e.error_code}: {e.error_message}")

if __name__ == '__main__':
    sub_client = SubscriptionClient(is_auto_connect=True,
                                    connection_delay_failure=10)
    sub_client.subscribe_price_depth_event("btcusdt", DepthStep.STEP0,
                                           callback, error)

It runs okay, and in case of broken internet connection it starts to reconnect, but never succed. When the internet connection comes back, it still try to reconnect with that error message: 'NoneType' object has no attribute 'is_ssl'

...
[Sub] call re_connect
In delay connection: 1
[Sub] call re_connect
In delay connection: 0
[Sub] call re_connect
[Sub][1] Reconnecting after 10 seconds later
[2020-06-03 00:10:26,691][__main__][ERROR]: SubscriptionError: Unexpected error: 'NoneType' object has no attribute 'is_ssl'
[Sub][1] Unexpected error: 'NoneType' object has no attribute 'is_ssl'
[Sub] call re_connect
In delay connection: 9
[Sub] call re_connect
In delay connection: 8
...

It looks like and internal bug, isn't it? Is there any options to bypass it and still have automatic reconnection available?