Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rc-0.4.2 #330

Merged
merged 4 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "viam-sdk"
version = "0.4.1"
version = "0.4.2"
description = "Viam Robotics Python SDK"
authors = [ "Naveed <[email protected]>" ]
license = "Apache-2.0"
Expand Down
21 changes: 12 additions & 9 deletions src/viam/sessions_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ def loop_kwargs():
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 +111,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 +146,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
Loading