Skip to content

Commit ea60b51

Browse files
authored
RSDK-6157 - add initial connection options (#347)
add initial connection options
1 parent baf82a2 commit ea60b51

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

Diff for: lib/src/robot/client.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class RobotClient {
126126
final client = RobotClient._();
127127
client._address = url;
128128
client._options = options;
129-
client._channel = await dial(url, options.dialOptions, () => client._sessionsClient.metadata());
129+
client._channel = await dialInitial(url, options.dialOptions, () => client._sessionsClient.metadata());
130130
client._sessionsClient = SessionsClient(client._channel, options.enableSessions, url);
131131
client._sessionsClient.start();
132132
client._client = rpb.RobotServiceClient(client._channel);

Diff for: lib/src/rpc/dial.dart

+36
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ class DialOptions {
6161
/// Whether the connection was made using mDNS
6262
bool _usingMdns = false;
6363

64+
/// The number of connection attempts to make when first dialing. If set to zero or a negative
65+
/// integer, will attempt to reconnect forever.
66+
int initialConnectionAttempts = 3;
67+
68+
// The timeout to use for initial connection attempts.
69+
Duration initialConnectionAttemptTimeout = const Duration(seconds: 10);
70+
6471
/// Timeout is the timeout for dial.
6572
Duration timeout = const Duration(seconds: 10);
6673

@@ -123,6 +130,35 @@ class DialWebRtcOptions {
123130
String? signalingAccessToken;
124131
}
125132

133+
/// {@category Viam SDK}
134+
/// Initial connection to a robot at the provided address with the given options, allowing for specifying of initial connection attempt count and timeout
135+
Future<ClientChannelBase> dialInitial(String address, DialOptions? options, String Function() sessionCallback) async {
136+
final opts = options ?? DialOptions();
137+
138+
int numAttempts = opts.initialConnectionAttempts;
139+
if (numAttempts == 0) {
140+
numAttempts = -1;
141+
}
142+
143+
final timeout = opts.timeout;
144+
opts.timeout = opts.initialConnectionAttemptTimeout;
145+
146+
while (numAttempts != 0) {
147+
try {
148+
final channel = await dial(address, opts, sessionCallback);
149+
opts.timeout = timeout;
150+
return channel;
151+
} catch (e) {
152+
numAttempts -= 1;
153+
if (numAttempts == 0) {
154+
rethrow;
155+
}
156+
}
157+
}
158+
159+
throw Exception('unreachable');
160+
}
161+
126162
/// {@category Viam SDK}
127163
/// Connect to a robot at the provided address with the given options
128164
Future<ClientChannelBase> dial(String address, DialOptions? options, String Function() sessionCallback) async {

0 commit comments

Comments
 (0)