Skip to content

Commit

Permalink
RSDK-3775 - Fix unawaited warning (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheukt committed Jun 26, 2023
1 parent 98dd941 commit 6584825
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions src/viam/sessions_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ def loop_kwargs():
return {"loop": asyncio.get_running_loop()}
return {}


async def delay(coro, seconds):
await asyncio.sleep(seconds, **loop_kwargs())
await coro


class SessionsClient:
"""
A Session allows a client to express that it is actively connected and
Expand Down Expand Up @@ -116,10 +110,22 @@ async def metadata(self) -> _MetadataLike:
self._heartbeat_interval = response.heartbeat_window.ToTimedelta()
self._current_id = response.id

# tick once to ensure heartbeats are supported
await self._heartbeat_tick()

if self._supported:
# We send heartbeats slightly faster than the interval window to
# ensure that we don't fall outside of it and expire the session.
wait = self._heartbeat_interval.total_seconds() / 5
asyncio.create_task(self._heartbeat_task(wait), name=f"{_TASK_PREFIX}-heartbeat")

return self._metadata

async def _heartbeat_task(self, wait: float):
while self._supported:
await asyncio.sleep(wait)
await self._heartbeat_tick()

async def _heartbeat_tick(self):
if not self._supported:
return
Expand All @@ -139,10 +145,6 @@ async def _heartbeat_tick(self):
self.reset()
else:
LOGGER.debug("Sent heartbeat successfully")
# We send heartbeats slightly faster than the interval window to
# ensure that we don't fall outside of it and expire the session.
wait = self._heartbeat_interval.total_seconds() / 5
asyncio.create_task(delay(self._heartbeat_tick(), wait), name=f"{_TASK_PREFIX}-heartbeat")

@property
def _metadata(self) -> _MetadataLike:
Expand Down

0 comments on commit 6584825

Please sign in to comment.