Skip to content

Commit

Permalink
RSDK-6041 - Add dial timeout option and default dial timeout (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
cheukt authored Jan 31, 2024
1 parent 429585e commit ea8f7cf
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
},
"homepage": "https://github.com/viamrobotics/viam-typescript-sdk#readme",
"dependencies": {
"@viamrobotics/rpc": "^0.2.2",
"@viamrobotics/rpc": "^0.2.3",
"exponential-backoff": "^3.1.1"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DIAL_TIMEOUT = 5000;
7 changes: 7 additions & 0 deletions src/robot/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
type DialOptions,
} from '@viamrobotics/rpc';
import { grpc } from '@improbable-eng/grpc-web';
import { DIAL_TIMEOUT } from '../constants';
import { DISCONNECTED, EventDispatcher, events, RECONNECTED } from '../events';
import proto from '../gen/robot/v1/robot_pb';
import type {
Expand Down Expand Up @@ -64,6 +65,10 @@ export interface ConnectOptions {
authEntity?: string;
creds?: Credentials;
priority?: number;

// set timeout in milliseconds for dialing. Default is defined by DIAL_TIMEOUT,
// and a value of 0 would disable the timeout.
dialTimeout?: number;
}

abstract class ServiceClient {
Expand Down Expand Up @@ -396,6 +401,7 @@ export class RobotClient extends EventDispatcher implements Robot {
authEntity = this.savedAuthEntity,
creds = this.savedCreds,
priority,
dialTimeout,
}: ConnectOptions = {}) {
if (this.connecting) {
// This lint is clearly wrong due to how the event loop works such that after an await, the condition may no longer be true.
Expand Down Expand Up @@ -429,6 +435,7 @@ export class RobotClient extends EventDispatcher implements Robot {
disableTrickleICE: false,
rtcConfig: this.webrtcOptions?.rtcConfig,
},
dialTimeout: dialTimeout ?? DIAL_TIMEOUT,
};

// Webrtcoptions will always be defined, but TS doesn't know this
Expand Down
15 changes: 14 additions & 1 deletion src/robot/dial.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DIAL_TIMEOUT } from '../constants';
import { RobotClient } from './client';

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

/** Check if a given number is a positive integer */
Expand Down Expand Up @@ -50,7 +54,11 @@ const dialDirect = async (conf: DialDirectConf): Promise<RobotClient> => {
if (conf.credential) {
creds = conf.credential;
}
await client.connect({ authEntity: conf.authEntity, creds });
await client.connect({
authEntity: conf.authEntity,
creds,
dialTimeout: conf.dialTimeout ?? DIAL_TIMEOUT,
});

// eslint-disable-next-line no-console
console.debug('connected via gRPC');
Expand Down Expand Up @@ -83,6 +91,10 @@ export interface DialWebRTCConf {
signalingAddress: string;
iceServers?: ICEServer[];
priority?: number;

// set timeout in milliseconds for dialing. Default is defined by DIAL_TIMEOUT,
// and a value of 0 would disable the timeout.
dialTimeout?: number;
}

const dialWebRTC = async (conf: DialWebRTCConf): Promise<RobotClient> => {
Expand Down Expand Up @@ -117,6 +129,7 @@ const dialWebRTC = async (conf: DialWebRTCConf): Promise<RobotClient> => {
authEntity: conf.authEntity ?? impliedURL,
creds,
priority: conf.priority,
dialTimeout: conf.dialTimeout ?? DIAL_TIMEOUT,
});

// eslint-disable-next-line no-console
Expand Down

0 comments on commit ea8f7cf

Please sign in to comment.