Skip to content

Commit c52a0c4

Browse files
authored
RSDK-5960 - initial connection options (#853)
1 parent 8a60553 commit c52a0c4

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/viam/robot/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
from viam.resource.registry import Registry
5353
from viam.resource.rpc_client_base import ReconfigurableResourceRPCClientBase, ResourceRPCClientBase
5454
from viam.resource.types import API, RESOURCE_TYPE_COMPONENT, RESOURCE_TYPE_SERVICE
55-
from viam.rpc.dial import DialOptions, ViamChannel, dial
55+
from viam.rpc.dial import DialOptions, ViamChannel, dial, _dial_inner
5656
from viam.services.service_base import ServiceBase
5757
from viam.sessions_client import SessionsClient
5858
from viam.utils import datetime_to_timestamp, dict_to_struct
@@ -403,7 +403,7 @@ async def _check_connection(self, check_every: int, reconnect_every: int):
403403
try:
404404
self._sessions_client.reset()
405405

406-
channel = await dial(self._address, self._options.dial_options)
406+
channel = await _dial_inner(self._address, self._options.dial_options)
407407

408408
client: RobotServiceStub
409409
if isinstance(channel, Channel):

src/viam/rpc/dial.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ class DialOptions:
7070
max_reconnect_attempts: int = 3
7171
"""Max number of times the client attempts to reconnect when connection is lost"""
7272

73+
initial_connection_attempts: int = 3
74+
"""Max number of times the client will attempt to establish an initial connection
75+
If set to a non-positive integer, then there will be no limit to initial connection attempts"""
76+
77+
initial_connection_attempt_timeout: float
78+
"""Number of seconds before dial connection times out on initial connection attempts
79+
Defaults to whatever value is set in the `timeout` field"""
80+
7381
timeout: float = 20
7482
"""Number of seconds before the dial connection times out
7583
Set to 20sec to match _defaultOfferDeadline in goutils/rpc/wrtc_call_queue.go"""
@@ -85,6 +93,8 @@ def __init__(
8593
allow_insecure_with_creds_downgrade: bool = False,
8694
max_reconnect_attempts: int = 3,
8795
timeout: float = 20,
96+
initial_connection_attempts: int = 3,
97+
initial_connection_attempt_timeout: Optional[float] = None,
8898
) -> None:
8999
self.disable_webrtc = disable_webrtc
90100
self.auth_entity = auth_entity
@@ -94,6 +104,8 @@ def __init__(
94104
self.allow_insecure_with_creds_downgrade = allow_insecure_with_creds_downgrade
95105
self.max_reconnect_attempts = max_reconnect_attempts
96106
self.timeout = timeout
107+
self.initial_connection_attempts = initial_connection_attempts
108+
self.initial_connection_attempt_timeout = initial_connection_attempt_timeout if initial_connection_attempt_timeout else timeout
97109

98110
@classmethod
99111
def with_api_key(cls, api_key: str, api_key_id: str) -> Self:
@@ -279,6 +291,27 @@ def free_str(self, ptr: ctypes.c_void_p):
279291

280292

281293
async def dial(address: str, options: Optional[DialOptions] = None) -> ViamChannel:
294+
options = options if options else DialOptions()
295+
timeout = options.timeout
296+
options.timeout = options.initial_connection_attempt_timeout
297+
if options.initial_connection_attempts == 0:
298+
options.initial_connection_attempts = -1
299+
attempt_countdown = options.initial_connection_attempts
300+
exception: Exception
301+
while attempt_countdown != 0:
302+
try:
303+
chan = await _dial_inner(address, options)
304+
options.timeout = timeout
305+
return chan
306+
except Exception as e:
307+
exception = e
308+
attempt_countdown -= 1
309+
# the only way we could get here is if we failed at least once which means we've set the
310+
# exception, so typechecker concerns about a possibly unbounded variable are unfounded
311+
raise exception # type: ignore
312+
313+
314+
async def _dial_inner(address: str, options: Optional[DialOptions] = None) -> ViamChannel:
282315
async def send_request(event: SendRequest):
283316
event.metadata["viam-client"] = f"python;v{SDK_VERSION};v{API_VERSION}"
284317

0 commit comments

Comments
 (0)