subscriptions: make generate sub_id more unique

This commit is contained in:
Jacob Plaster
2019-01-24 13:52:01 +00:00
committed by Jacob Plaster
parent afbb4a670c
commit 3eb02910d2
4 changed files with 20 additions and 4 deletions

View File

@@ -4,7 +4,15 @@ Module used to describe all of the different data types
import time
import json
from random import randint
def generate_sub_id():
"""
Generates a unique id in the form of 12345566-12334556
"""
prefix = str(int(round(time.time() * 1000)))
suffix = str(randint(0, 9999999))
return "{}-{}".format(prefix, suffix)
class Subscription:
"""
@@ -23,7 +31,7 @@ class Subscription:
self.chan_id = None
if timeframe:
self.key = 'trade:{}:{}'.format(self.timeframe, self.symbol)
self.sub_id = int(round(time.time() * 1000))
self.sub_id = generate_sub_id()
self.send_payload = self._generate_payload(**kwargs)
def confirm_subscription(self, chan_id):

View File

@@ -4,7 +4,15 @@ Module used to describe all of the different data types
import time
import json
from random import randint
def generate_sub_id():
"""
Generates a unique id in the form of 12345566-12334556
"""
prefix = str(int(round(time.time() * 1000)))
suffix = str(randint(0, 9999999))
return "{}-{}".format(prefix, suffix)
class Subscription:
"""
@@ -23,7 +31,7 @@ class Subscription:
self.chan_id = None
if timeframe:
self.key = 'trade:{}:{}'.format(self.timeframe, self.symbol)
self.sub_id = int(round(time.time() * 1000))
self.sub_id = generate_sub_id()
self.send_payload = self._generate_payload(**kwargs)
def confirm_subscription(self, chan_id):

View File

@@ -56,7 +56,7 @@ class GenericWebsocket:
async def _connect(self, host):
async with websockets.connect(host) as websocket:
self.ws = websocket
self.logger.info("Wesocket connected to {}".format(self.host))
self.logger.info("Wesocket connected to {}".format(host))
while True:
await asyncio.sleep(0)
message = await websocket.recv()

View File

@@ -63,11 +63,11 @@ class SubscriptionManager:
chan_id = raw_ws_data.get("chanId")
sub = self.subscriptions_chanid[chan_id]
sub.confirm_unsubscribe()
self.bfxapi._emit('unsubscribed', sub)
# call onComplete callback if exists
if sub.sub_id in self.unsubscribe_callbacks:
await self.unsubscribe_callbacks[sub.sub_id]()
del self.unsubscribe_callbacks[sub.sub_id]
self.bfxapi._emit('unsubscribed', sub)
def get(self, chan_id):
return self.subscriptions_chanid[chan_id]