Skip to content

Add the ability to abort an initial connection attempt #327

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/vanilla/package-lock.json

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

6 changes: 6 additions & 0 deletions examples/vanilla/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const disconnectEl = <HTMLButtonElement>document.getElementById('disconnect');
const resourcesEl = <HTMLButtonElement>document.getElementById('resources');

let machine: VIAM.RobotClient | undefined = undefined;
const reconnectAbortSignal = { abort: false };

const handleConnectionStateChange = (event: unknown) => {
updateConnectionStatus(
Expand Down Expand Up @@ -42,6 +43,7 @@ const connect = async () => {
return;
}

reconnectAbortSignal.abort = false;
updateConnectionStatus(VIAM.MachineConnectionEvent.CONNECTING);

try {
Expand All @@ -53,6 +55,7 @@ const connect = async () => {
},
authEntity: API_KEY_ID,
signalingAddress: 'https://app.viam.com:443',
reconnectAbortSignal,
});
updateConnectionStatus(VIAM.MachineConnectionEvent.CONNECTED);
machine.on('connectionstatechange', handleConnectionStateChange);
Expand All @@ -62,6 +65,9 @@ const connect = async () => {
};

const disconnect = async () => {
// If currently establishing initial connection, abort.
reconnectAbortSignal.abort = true;

if (!machine) {
return;
}
Expand Down
25 changes: 15 additions & 10 deletions src/robot/dial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface DialDirectConf {
noReconnect?: boolean;
reconnectMaxAttempts?: number;
reconnectMaxWait?: number;
reconnectAbortSignal?: { abort: boolean };
// set timeout in milliseconds for dialing. Default is defined by DIAL_TIMEOUT,
// and a value of 0 would disable the timeout.
dialTimeout?: number;
Expand Down Expand Up @@ -88,6 +89,7 @@ export interface DialWebRTCConf {
noReconnect?: boolean;
reconnectMaxAttempts?: number;
reconnectMaxWait?: number;
reconnectAbortSignal?: { abort: boolean };
// WebRTC
signalingAddress: string;
iceServers?: ICEServer[];
Expand Down Expand Up @@ -178,13 +180,14 @@ export const createRobotClient = async (
retry: (_error, attemptNumber) => {
// eslint-disable-next-line no-console
console.debug(`Failed to connect, attempt ${attemptNumber} with backoff`);
// Always retry the next attempt
return true;

// Abort reconnects if the the caller specifies, otherwise retry
return !conf.reconnectAbortSignal?.abort;
},
};

// Try to dial via WebRTC first.
if (isDialWebRTCConf(conf)) {
if (isDialWebRTCConf(conf) && !conf.reconnectAbortSignal?.abort) {
try {
return conf.noReconnect
? await dialWebRTC(conf)
Expand All @@ -195,13 +198,15 @@ export const createRobotClient = async (
}
}

try {
return conf.noReconnect
? await dialDirect(conf)
: await backOff(async () => dialDirect(conf), backOffOpts);
} catch {
// eslint-disable-next-line no-console
console.debug('Failed to connect via gRPC');
if (!conf.reconnectAbortSignal?.abort) {
try {
return conf.noReconnect
? await dialDirect(conf)
: await backOff(async () => dialDirect(conf), backOffOpts);
} catch {
// eslint-disable-next-line no-console
console.debug('Failed to connect via gRPC');
}
}

throw new Error('Failed to connect to robot');
Expand Down