Skip to content

Commit ea8f7cf

Browse files
authored
RSDK-6041 - Add dial timeout option and default dial timeout (#235)
1 parent 429585e commit ea8f7cf

File tree

5 files changed

+27
-6
lines changed

5 files changed

+27
-6
lines changed

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
},
4646
"homepage": "https://github.com/viamrobotics/viam-typescript-sdk#readme",
4747
"dependencies": {
48-
"@viamrobotics/rpc": "^0.2.2",
48+
"@viamrobotics/rpc": "^0.2.3",
4949
"exponential-backoff": "^3.1.1"
5050
},
5151
"devDependencies": {

src/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const DIAL_TIMEOUT = 5000;

src/robot/client.ts

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
type DialOptions,
99
} from '@viamrobotics/rpc';
1010
import { grpc } from '@improbable-eng/grpc-web';
11+
import { DIAL_TIMEOUT } from '../constants';
1112
import { DISCONNECTED, EventDispatcher, events, RECONNECTED } from '../events';
1213
import proto from '../gen/robot/v1/robot_pb';
1314
import type {
@@ -64,6 +65,10 @@ export interface ConnectOptions {
6465
authEntity?: string;
6566
creds?: Credentials;
6667
priority?: number;
68+
69+
// set timeout in milliseconds for dialing. Default is defined by DIAL_TIMEOUT,
70+
// and a value of 0 would disable the timeout.
71+
dialTimeout?: number;
6772
}
6873

6974
abstract class ServiceClient {
@@ -396,6 +401,7 @@ export class RobotClient extends EventDispatcher implements Robot {
396401
authEntity = this.savedAuthEntity,
397402
creds = this.savedCreds,
398403
priority,
404+
dialTimeout,
399405
}: ConnectOptions = {}) {
400406
if (this.connecting) {
401407
// This lint is clearly wrong due to how the event loop works such that after an await, the condition may no longer be true.
@@ -429,6 +435,7 @@ export class RobotClient extends EventDispatcher implements Robot {
429435
disableTrickleICE: false,
430436
rtcConfig: this.webrtcOptions?.rtcConfig,
431437
},
438+
dialTimeout: dialTimeout ?? DIAL_TIMEOUT,
432439
};
433440

434441
// Webrtcoptions will always be defined, but TS doesn't know this

src/robot/dial.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DIAL_TIMEOUT } from '../constants';
12
import { RobotClient } from './client';
23

34
interface Credential {
@@ -14,6 +15,9 @@ export interface DialDirectConf {
1415
noReconnect?: boolean;
1516
reconnectMaxAttempts?: number;
1617
reconnectMaxWait?: number;
18+
// set timeout in milliseconds for dialing. Default is defined by DIAL_TIMEOUT,
19+
// and a value of 0 would disable the timeout.
20+
dialTimeout?: number;
1721
}
1822

1923
/** Check if a given number is a positive integer */
@@ -50,7 +54,11 @@ const dialDirect = async (conf: DialDirectConf): Promise<RobotClient> => {
5054
if (conf.credential) {
5155
creds = conf.credential;
5256
}
53-
await client.connect({ authEntity: conf.authEntity, creds });
57+
await client.connect({
58+
authEntity: conf.authEntity,
59+
creds,
60+
dialTimeout: conf.dialTimeout ?? DIAL_TIMEOUT,
61+
});
5462

5563
// eslint-disable-next-line no-console
5664
console.debug('connected via gRPC');
@@ -83,6 +91,10 @@ export interface DialWebRTCConf {
8391
signalingAddress: string;
8492
iceServers?: ICEServer[];
8593
priority?: number;
94+
95+
// set timeout in milliseconds for dialing. Default is defined by DIAL_TIMEOUT,
96+
// and a value of 0 would disable the timeout.
97+
dialTimeout?: number;
8698
}
8799

88100
const dialWebRTC = async (conf: DialWebRTCConf): Promise<RobotClient> => {
@@ -117,6 +129,7 @@ const dialWebRTC = async (conf: DialWebRTCConf): Promise<RobotClient> => {
117129
authEntity: conf.authEntity ?? impliedURL,
118130
creds,
119131
priority: conf.priority,
132+
dialTimeout: conf.dialTimeout ?? DIAL_TIMEOUT,
120133
});
121134

122135
// eslint-disable-next-line no-console

0 commit comments

Comments
 (0)