99
1010
1111class ConnectSpotWebsocket :
12- MAX_RECONNECTS = 5
12+ MAX_SEND_MESSAGE_RETRIES = 5
1313 MAX_RECONNECT_SECONDS = 60
1414
1515 def __init__ (self , client , endpoint : str , callback = None , private : bool = False , beta : bool = False ):
@@ -30,6 +30,8 @@ def __init__(self, client, endpoint: str, callback=None, private: bool=False, be
3030
3131 asyncio .ensure_future (self .run_forever (), loop = asyncio .get_running_loop ())
3232
33+ self .connected = False
34+
3335 @property
3436 def subscriptions (self ) -> list :
3537 return self ._subscriptions
@@ -51,6 +53,7 @@ async def _run(self, event: asyncio.Event):
5153 if self .private : self ._client .websocket_priv = self
5254 else : self ._client .websocket_pub = self
5355
56+ self .connected = True
5457 self ._socket = socket
5558 self ._reconnect_num = 0
5659
@@ -85,9 +88,9 @@ async def _reconnect(self):
8588
8689 self ._reconnect_num += 1
8790 reconnect_wait = self ._get_reconnect_wait (self ._reconnect_num )
88- logging .info (f'asyncio sleep reconnect_wait={ reconnect_wait } s reconnect_num={ self ._reconnect_num } ' )
91+ logging .debug (f'asyncio sleep reconnect_wait={ reconnect_wait } s reconnect_num={ self ._reconnect_num } ' )
8992 await asyncio .sleep (reconnect_wait )
90- logging .info (f'asyncio sleep ok ' )
93+ logging .debug (f'asyncio sleep done ' )
9194 event = asyncio .Event ()
9295
9396 tasks = {
@@ -143,9 +146,9 @@ async def send_ping(self):
143146
144147 async def send_message (self , msg , private : bool = False , retry_count : int = 0 ):
145148 logging .info (f'send_message (private: { private } ; tries: { retry_count } ): { msg } ' )
146- if not self ._socket :
147- if retry_count < self .MAX_RECONNECTS :
148- await asyncio .sleep (1 )
149+ if not self ._socket : # if not connected
150+ if retry_count < self .MAX_SEND_MESSAGE_RETRIES :
151+ await asyncio .sleep (2 )
149152 await self .send_message (msg , private = private , retry_count = retry_count + 1 )
150153 else :
151154 msg ['reqid' ] = int (time .time () * 1000 )
@@ -165,7 +168,7 @@ class KrakenSpotWSClient(object):
165168 def __init__ (self , client , callback = None , beta : bool = False ):
166169 self ._callback = callback
167170 self ._client = client
168-
171+
169172 self ._pub_conn = ConnectSpotWebsocket (
170173 client = self ._client ,
171174 endpoint = self .PROD_ENV_URL if not beta else BETA_ENV_URL ,
0 commit comments