async def get_status(ident, msg): msg_orig = msg msg_bytes = pack_msg(msg) res = [] for name in sorted(g.up): d = g.up[name] await d["sock_deal"].send_multipart(ident + [b"", msg_bytes]) msg_parts = await d["sock_deal_pub"].poll_for_msg_id(msg["msg_id"]) msg = unpack_msg(msg_parts[-1]) error.check_message(msg) content = msg["content"] res += content status = { "name": MODULE_NAME, "uptime": (datetime.utcnow() - g.startup_time).total_seconds(), "pub_bytes": g.pub_bytes, "num_clients": len(g.clients), "num_subscribed_tickers": sum([bool(x["subscriptions"]) for x in g.tickers.values()]), "reaper_interval": g.reaper_interval, "reaper_max_heartbeats": g.reaper_max_heartbeats, } content = [status, res] msg_orig["content"] = content await g.sock_ctl.send_multipart(ident + [b"", pack_msg(msg_orig)])
async def reap_client(client, d): ticker_ids = list(d["subscriptions"].keys()) for ticker_id in ticker_ids: msg = await update_subscription(client, ticker_id, SubscriptionDefinition()) error.check_message(msg) del g.clients[client]
def send_recv_command_raw(cmd, content): sock = g.sock_req msg = dict(command=cmd, content=content) msg_bytes = pack_msg(msg) sock.send(msg_bytes) msg_parts = sock.recv_multipart() msg = json.loads(msg_parts[-1].decode()) error.check_message(msg) return msg["content"]
async def send_recv_init_cmd(command, content=None): msg = {"command": command} msg["msg_id"] = msg_id = str(uuid.uuid4()) if content: msg["content"] = content msg_bytes = (" " + json.dumps(msg)).encode() await g.sock_deal.send_multipart([b"", msg_bytes]) msg_parts = await g.sock_deal.recv_multipart() msg = msg_parts[-1] msg = json.loads(msg.decode()) error.check_message(msg) return msg
async def send_recv_command_raw(sock, msg): msg_bytes = pack_msg(msg) msg_id = msg["msg_id"] await sock.send_multipart([b"", msg_bytes]) # this will wipe the message queue while True: msg_parts = await sock.recv_multipart() try: msg = json.loads(msg_parts[-1].decode()) if msg["msg_id"] == msg_id: break except: pass error.check_message(msg) return msg["content"]
async def get_ticker_info(ticker_id: str): msg_id = str(uuid.uuid4()) data = { "command": "get_ticker_info", "msg_id": msg_id, "content": { "ticker": { "ticker_id": ticker_id } } } msg = " " + json.dumps(data) msg = msg.encode() await g.sock_deal.send_multipart([b"", msg]) msg_parts = await g.sock_deal_pub.poll_for_msg_id(msg_id) msg = json.loads(msg_parts[1].decode()) error.check_message(msg) content = msg["content"] assert len(content) == 1, len(content) return content[0]