def wait_reconnect_millisecond(self): wait_millisecond = int(self.reconnect_after_ms - self.heart_beat_limit_ms) now_ms = get_current_timestamp() wait_millisecond = wait_millisecond if wait_millisecond else 1000 # job loop after 1 second return (wait_millisecond + now_ms)
def watch_dog_job(*args): watch_dog_obj = args[0] for idx, websocket_manage in enumerate( watch_dog_obj.websocket_manage_list): if websocket_manage.request.auto_close == True: # setting auto close no need reconnect pass elif websocket_manage.state == ConnectionState.CONNECTED: if watch_dog_obj.is_auto_connect: ts = get_current_timestamp( ) - websocket_manage.last_receive_time if ts > watch_dog_obj.heart_beat_limit_ms: watch_dog_obj.logger.warning("[Sub][" + str(websocket_manage.id) + "] No response from server") websocket_manage.close_and_wait_reconnect( watch_dog_obj.wait_reconnect_millisecond()) elif websocket_manage.state == ConnectionState.WAIT_RECONNECT: watch_dog_obj.logger.warning("[Sub] call re_connect") websocket_manage.re_connect() pass elif websocket_manage.state == ConnectionState.CLOSED_ON_ERROR: if watch_dog_obj.is_auto_connect: websocket_manage.close_and_wait_reconnect( watch_dog_obj.reconnect_after_ms) pass
def request_order_detail_channel(order_id, client_req_id = None): channel = dict() channel["op"] = "req" channel["topic"] = "orders.detail" channel["order-id"] = str(order_id) channel["cid"] = str(client_req_id) if client_req_id else str(get_current_timestamp()) return json.dumps(channel)
def request_kline_channel(symbol, interval, from_ts_second = None, to_ts_second = None): channel = dict() channel["req"] = "market." + symbol + ".kline." + interval channel["id"] = str(get_current_timestamp()) if from_ts_second: channel["from"] = int(from_ts_second) if to_ts_second: channel["to"] = int(to_ts_second) return json.dumps(channel)
def request_order_list_channel(symbol, account_id, states_str= None, client_req_id = None, more_key={}): channel = dict() try: channel["op"] = "req" channel["account-id"] = account_id channel["topic"] = "orders.list" channel["symbol"] = symbol if states_str and len(states_str): channel["states"] = str(states_str) channel["cid"] = str(client_req_id) if client_req_id else str(get_current_timestamp()) channel = dict_add_new(channel, more_key) except Exception as e: print(e) return json.dumps(channel)
def check_reconnect(watch_dog: 'WatchDog'): for name, websocket_manage in watch_dog.websocket_manage_dict.items(): ts = get_current_timestamp() if not hasattr(websocket_manage, 'created_at'): setattr(websocket_manage, 'created_at', ts) if websocket_manage.request.auto_close: # setting auto close no need reconnect pass elif websocket_manage.state == ConnectionState.CONNECTED: if watch_dog.is_auto_connect: if ts > websocket_manage.last_receive_time + watch_dog.heart_beat_limit_ms: watch_dog.logger.warning(f"[{name}] No response from server") close_and_wait_reconnect(websocket_manage, watch_dog.wait_reconnect_millisecond()) elif ts > watch_dog.get_random_restart_at(websocket_manage): close_and_wait_reconnect(websocket_manage, ts+100) elif websocket_manage.state == ConnectionState.WAIT_RECONNECT: if ts > websocket_manage.reconnect_at: watch_dog.logger.warning(f"[{name}] Reconnect") websocket_manage.state = ConnectionState.RECONNECTING websocket_manage.re_connect() websocket_manage.created_at = ts repeat_connection = [ conn for conn, wm in WEBSOCKET_CONNECTION_HANDLER.items() if wm == websocket_manage and conn != wm.original_connection ] for conn in repeat_connection: conn.close() del WEBSOCKET_CONNECTION_HANDLER[conn] elif websocket_manage.state == ConnectionState.CLOSED_ON_ERROR: if watch_dog.is_auto_connect: close_and_wait_reconnect(websocket_manage, ts + watch_dog.reconnect_after_ms)
def request_account_list_channel(client_req_id = None): channel = dict() channel["op"] = "req" channel["topic"] = "accounts.list" channel["cid"] = str(client_req_id) if client_req_id else str(get_current_timestamp()) return json.dumps(channel)
def request_market_detail_channel(symbol): channel = dict() channel["req"] = "market." + symbol + ".detail" channel["id"] = str(get_current_timestamp()) return json.dumps(channel)
def request_price_depth_channel(symbol, step_type = "step0"): channel = dict() channel["req"] = "market." + symbol + ".depth." + step_type channel["id"] = str(get_current_timestamp()) return json.dumps(channel)
def request_mbp_channel(symbol, levels): channel = dict() channel["req"] = "market.{symbol}.mbp.{levels}".format(symbol=symbol, levels=levels) channel["id"] = str(get_current_timestamp()) return json.dumps(channel)
def mbp_full_channel(symbol, levels): channel = dict() channel["sub"] = "market.{symbol}.mbp.refresh.{levels}".format( symbol=symbol, levels=levels) channel["id"] = str(get_current_timestamp()) return json.dumps(channel)
def kline_channel(symbol, interval): channel = dict() channel["sub"] = "market." + symbol + ".kline." + interval channel["id"] = str(get_current_timestamp()) return json.dumps(channel)
def price_depth_bbo_channel(symbol): channel = dict() channel["sub"] = "market." + symbol + ".bbo" channel["id"] = str(get_current_timestamp()) return json.dumps(channel)
def price_depth_channel(symbol, step_type=DepthStep.STEP0): channel = dict() channel["sub"] = "market." + symbol + ".depth." + step_type channel["id"] = str(get_current_timestamp()) return json.dumps(channel)
def trade_detail_channel(symbol): channel = dict() channel["sub"] = "market." + symbol + ".trade.detail" channel["id"] = str(get_current_timestamp()) return json.dumps(channel)