@@ -165,7 +165,7 @@ async def __restart(self):
165
165
if self .__task :
166
166
self .__task : Task
167
167
self .__task .cancel ()
168
- await self ._client . close ()
168
+ self ._client = None # clear pending waits
169
169
self .__heartbeater .event .clear ()
170
170
await self ._establish_connection ()
171
171
@@ -199,6 +199,10 @@ async def _establish_connection(
199
199
200
200
if stream is None :
201
201
continue
202
+ if self ._client is None :
203
+ await self ._establish_connection ()
204
+ break
205
+
202
206
if self ._client .close_code in range (4010 , 4014 ) or self ._client .close_code == 4004 :
203
207
raise GatewayException (self ._client .close_code )
204
208
@@ -230,6 +234,10 @@ async def _handle_connection(
230
234
if op == OpCodeType .HELLO :
231
235
self .__heartbeater .delay = data ["heartbeat_interval" ]
232
236
self .__heartbeater .event .set ()
237
+
238
+ if self .__task :
239
+ self .__task .cancel () # so we can reduce redundant heartbeat bg tasks.
240
+
233
241
self .__task = ensure_future (self ._manage_heartbeat ())
234
242
235
243
if not self .session_id :
@@ -246,12 +254,17 @@ async def _handle_connection(
246
254
if op in (OpCodeType .INVALIDATE_SESSION , OpCodeType .RECONNECT ):
247
255
log .debug ("INVALID_SESSION/RECONNECT" )
248
256
249
- if data and op != OpCodeType .RECONNECT :
257
+ # if data and op != OpCodeType.RECONNECT:
258
+ # self.session_id = None
259
+ # self.sequence = None
260
+ # self._closed = True
261
+
262
+ if bool (data ) is False and op == OpCodeType .INVALIDATE_SESSION :
250
263
self .session_id = None
251
- self .sequence = None
252
- self ._closed = True
253
264
254
265
await self .__restart ()
266
+ elif event == "RESUMED" :
267
+ log .debug (f"RESUMED (session_id: { self .session_id } , seq: { self .sequence } )" )
255
268
elif event == "READY" :
256
269
self ._ready = data
257
270
self .session_id = data ["session_id" ]
@@ -515,6 +528,9 @@ def __option_type_context(self, context: object, type: int) -> dict:
515
528
}
516
529
return _resolved
517
530
531
+ async def restart (self ):
532
+ await self .__restart ()
533
+
518
534
@property
519
535
async def __receive_packet_stream (self ) -> Optional [Dict [str , Any ]]:
520
536
"""
@@ -523,6 +539,7 @@ async def __receive_packet_stream(self) -> Optional[Dict[str, Any]]:
523
539
:return: The packet stream.
524
540
:rtype: Optional[Dict[str, Any]]
525
541
"""
542
+
526
543
packet : WSMessage = await self ._client .receive ()
527
544
return loads (packet .data ) if packet and isinstance (packet .data , str ) else None
528
545
0 commit comments