From 202f7705199cbacd43fcb26dea7aefe2bd03f341 Mon Sep 17 00:00:00 2001 From: Ethan Look Date: Mon, 29 Jul 2024 11:49:01 -0400 Subject: [PATCH 1/2] Add the ability to abort an initial connection attempt --- examples/vanilla/package-lock.json | 2 +- examples/vanilla/src/main.ts | 6 ++++++ src/robot/dial.ts | 25 +++++++++++++++---------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/examples/vanilla/package-lock.json b/examples/vanilla/package-lock.json index b432016f4..b12d8c48b 100644 --- a/examples/vanilla/package-lock.json +++ b/examples/vanilla/package-lock.json @@ -19,7 +19,7 @@ }, "../..": { "name": "@viamrobotics/sdk", - "version": "0.16.0", + "version": "0.19.1", "license": "Apache-2.0", "dependencies": { "@viamrobotics/rpc": "^0.2.5", diff --git a/examples/vanilla/src/main.ts b/examples/vanilla/src/main.ts index 89d06dcef..9fccdda2f 100644 --- a/examples/vanilla/src/main.ts +++ b/examples/vanilla/src/main.ts @@ -12,6 +12,7 @@ const disconnectEl = document.getElementById('disconnect'); const resourcesEl = document.getElementById('resources'); let machine: VIAM.RobotClient | undefined = undefined; +const reconnectAbort = { abort: false }; const handleConnectionStateChange = (event: unknown) => { updateConnectionStatus( @@ -42,6 +43,7 @@ const connect = async () => { return; } + reconnectAbort.abort = false; updateConnectionStatus(VIAM.MachineConnectionEvent.CONNECTING); try { @@ -53,6 +55,7 @@ const connect = async () => { }, authEntity: API_KEY_ID, signalingAddress: 'https://app.viam.com:443', + reconnectAbort, }); updateConnectionStatus(VIAM.MachineConnectionEvent.CONNECTED); machine.on('connectionstatechange', handleConnectionStateChange); @@ -62,6 +65,9 @@ const connect = async () => { }; const disconnect = async () => { + // If currently establishing initial connection, abort. + reconnectAbort.abort = true; + if (!machine) { return; } diff --git a/src/robot/dial.ts b/src/robot/dial.ts index 09d3349ac..bd0a3056b 100644 --- a/src/robot/dial.ts +++ b/src/robot/dial.ts @@ -16,6 +16,7 @@ export interface DialDirectConf { noReconnect?: boolean; reconnectMaxAttempts?: number; reconnectMaxWait?: number; + reconnectAbort?: { 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; @@ -88,6 +89,7 @@ export interface DialWebRTCConf { noReconnect?: boolean; reconnectMaxAttempts?: number; reconnectMaxWait?: number; + reconnectAbort?: { abort: boolean }; // WebRTC signalingAddress: string; iceServers?: ICEServer[]; @@ -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.reconnectAbort?.abort; }, }; // Try to dial via WebRTC first. - if (isDialWebRTCConf(conf)) { + if (isDialWebRTCConf(conf) && !conf.reconnectAbort?.abort) { try { return conf.noReconnect ? await dialWebRTC(conf) @@ -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.reconnectAbort?.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'); From c8d93fb8a8e32552844ba400b2832520184596c7 Mon Sep 17 00:00:00 2001 From: Ethan Look Date: Fri, 2 Aug 2024 12:57:16 -0400 Subject: [PATCH 2/2] reconnectAbort > reconnectAbortSignal --- examples/vanilla/src/main.ts | 8 ++++---- src/robot/dial.ts | 10 +++++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/vanilla/src/main.ts b/examples/vanilla/src/main.ts index 9fccdda2f..618cd16d8 100644 --- a/examples/vanilla/src/main.ts +++ b/examples/vanilla/src/main.ts @@ -12,7 +12,7 @@ const disconnectEl = document.getElementById('disconnect'); const resourcesEl = document.getElementById('resources'); let machine: VIAM.RobotClient | undefined = undefined; -const reconnectAbort = { abort: false }; +const reconnectAbortSignal = { abort: false }; const handleConnectionStateChange = (event: unknown) => { updateConnectionStatus( @@ -43,7 +43,7 @@ const connect = async () => { return; } - reconnectAbort.abort = false; + reconnectAbortSignal.abort = false; updateConnectionStatus(VIAM.MachineConnectionEvent.CONNECTING); try { @@ -55,7 +55,7 @@ const connect = async () => { }, authEntity: API_KEY_ID, signalingAddress: 'https://app.viam.com:443', - reconnectAbort, + reconnectAbortSignal, }); updateConnectionStatus(VIAM.MachineConnectionEvent.CONNECTED); machine.on('connectionstatechange', handleConnectionStateChange); @@ -66,7 +66,7 @@ const connect = async () => { const disconnect = async () => { // If currently establishing initial connection, abort. - reconnectAbort.abort = true; + reconnectAbortSignal.abort = true; if (!machine) { return; diff --git a/src/robot/dial.ts b/src/robot/dial.ts index bd0a3056b..5d1a7c6a2 100644 --- a/src/robot/dial.ts +++ b/src/robot/dial.ts @@ -16,7 +16,7 @@ export interface DialDirectConf { noReconnect?: boolean; reconnectMaxAttempts?: number; reconnectMaxWait?: number; - reconnectAbort?: { abort: boolean }; + 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; @@ -89,7 +89,7 @@ export interface DialWebRTCConf { noReconnect?: boolean; reconnectMaxAttempts?: number; reconnectMaxWait?: number; - reconnectAbort?: { abort: boolean }; + reconnectAbortSignal?: { abort: boolean }; // WebRTC signalingAddress: string; iceServers?: ICEServer[]; @@ -182,12 +182,12 @@ export const createRobotClient = async ( console.debug(`Failed to connect, attempt ${attemptNumber} with backoff`); // Abort reconnects if the the caller specifies, otherwise retry - return !conf.reconnectAbort?.abort; + return !conf.reconnectAbortSignal?.abort; }, }; // Try to dial via WebRTC first. - if (isDialWebRTCConf(conf) && !conf.reconnectAbort?.abort) { + if (isDialWebRTCConf(conf) && !conf.reconnectAbortSignal?.abort) { try { return conf.noReconnect ? await dialWebRTC(conf) @@ -198,7 +198,7 @@ export const createRobotClient = async ( } } - if (!conf.reconnectAbort?.abort) { + if (!conf.reconnectAbortSignal?.abort) { try { return conf.noReconnect ? await dialDirect(conf)