@@ -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
281293async 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