Skip to content

RSDK-3961: Add max_reconnect_attempts #418

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

Merged
merged 2 commits into from
Sep 13, 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
7 changes: 5 additions & 2 deletions src/viam/robot/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ async def _check_connection(self, check_every: int, reconnect_every: int):

# Failure to grab resources could be for spurious, non-networking reasons. Try three times just to be safe.
connection_error = None
for attempt in range(3):
for _ in range(3):
Copy link
Member Author

@hexbabe hexbabe Sep 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I revised this to mirror the loop structures since this attempt iterator is unused anyways

try:
_: ResourceNamesResponse = await self._client.ResourceNames(ResourceNamesRequest(), timeout=1)
connection_error = None
Expand All @@ -283,7 +283,9 @@ async def _check_connection(self, check_every: int, reconnect_every: int):
if reconnect_every <= 0:
continue

while not self._connected:
reconnect_attempts = self._options.dial_options.max_reconnect_attempts if self._options.dial_options else 3

for _ in range(reconnect_attempts):
try:
self._sessions_client.reset()

Expand All @@ -308,6 +310,7 @@ async def _check_connection(self, check_every: int, reconnect_every: int):
await self.refresh()
self._connected = True
LOGGER.debug("Successfully reconnected robot")
break
except Exception as e:
LOGGER.error(f"Failed to reconnect, trying again in {reconnect_every}sec", exc_info=e)
self._sessions_client.reset()
Expand Down
5 changes: 5 additions & 0 deletions src/viam/rpc/dial.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ class DialOptions:
if detected, even with credentials present. This is generally
unsafe to use, but can be requested."""

max_reconnect_attempts: int = 3
"""Max number of times the client attempts to reconnect when connection is lost"""

def __init__(
self,
disable_webrtc: bool = False,
Expand All @@ -74,13 +77,15 @@ def __init__(
insecure: bool = False,
allow_insecure_downgrade: bool = False,
allow_insecure_with_creds_downgrade: bool = False,
max_reconnect_attempts: int = 3,
) -> None:
self.disable_webrtc = disable_webrtc
self.auth_entity = auth_entity
self.credentials = credentials
self.insecure = insecure
self.allow_insecure_downgrade = allow_insecure_downgrade
self.allow_insecure_with_creds_downgrade = allow_insecure_with_creds_downgrade
self.max_reconnect_attempts = max_reconnect_attempts


def _host_port_from_url(url) -> Tuple[Optional[str], Optional[int]]:
Expand Down