@@ -70,6 +70,14 @@ class DialOptions:
70
70
max_reconnect_attempts : int = 3
71
71
"""Max number of times the client attempts to reconnect when connection is lost"""
72
72
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
+
73
81
timeout : float = 20
74
82
"""Number of seconds before the dial connection times out
75
83
Set to 20sec to match _defaultOfferDeadline in goutils/rpc/wrtc_call_queue.go"""
@@ -85,6 +93,8 @@ def __init__(
85
93
allow_insecure_with_creds_downgrade : bool = False ,
86
94
max_reconnect_attempts : int = 3 ,
87
95
timeout : float = 20 ,
96
+ initial_connection_attempts : int = 3 ,
97
+ initial_connection_attempt_timeout : Optional [float ] = None ,
88
98
) -> None :
89
99
self .disable_webrtc = disable_webrtc
90
100
self .auth_entity = auth_entity
@@ -94,6 +104,8 @@ def __init__(
94
104
self .allow_insecure_with_creds_downgrade = allow_insecure_with_creds_downgrade
95
105
self .max_reconnect_attempts = max_reconnect_attempts
96
106
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
97
109
98
110
@classmethod
99
111
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):
279
291
280
292
281
293
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 :
282
315
async def send_request (event : SendRequest ):
283
316
event .metadata ["viam-client" ] = f"python;v{ SDK_VERSION } ;v{ API_VERSION } "
284
317
0 commit comments