@@ -35,12 +35,6 @@ def loop_kwargs():
3535 return {"loop" : asyncio .get_running_loop ()}
3636 return {}
3737
38-
39- async def delay (coro , seconds ):
40- await asyncio .sleep (seconds , ** loop_kwargs ())
41- await coro
42-
43-
4438class SessionsClient :
4539 """
4640 A Session allows a client to express that it is actively connected and
@@ -116,10 +110,22 @@ async def metadata(self) -> _MetadataLike:
116110 self ._heartbeat_interval = response .heartbeat_window .ToTimedelta ()
117111 self ._current_id = response .id
118112
113+ # tick once to ensure heartbeats are supported
119114 await self ._heartbeat_tick ()
120115
116+ if self ._supported :
117+ # We send heartbeats slightly faster than the interval window to
118+ # ensure that we don't fall outside of it and expire the session.
119+ wait = self ._heartbeat_interval .total_seconds () / 5
120+ asyncio .create_task (self ._heartbeat_task (wait ), name = f"{ _TASK_PREFIX } -heartbeat" )
121+
121122 return self ._metadata
122123
124+ async def _heartbeat_task (self , wait : float ):
125+ while self ._supported :
126+ await asyncio .sleep (wait )
127+ await self ._heartbeat_tick ()
128+
123129 async def _heartbeat_tick (self ):
124130 if not self ._supported :
125131 return
@@ -139,10 +145,6 @@ async def _heartbeat_tick(self):
139145 self .reset ()
140146 else :
141147 LOGGER .debug ("Sent heartbeat successfully" )
142- # We send heartbeats slightly faster than the interval window to
143- # ensure that we don't fall outside of it and expire the session.
144- wait = self ._heartbeat_interval .total_seconds () / 5
145- asyncio .create_task (delay (self ._heartbeat_tick (), wait ), name = f"{ _TASK_PREFIX } -heartbeat" )
146148
147149 @property
148150 def _metadata (self ) -> _MetadataLike :
0 commit comments