-
Notifications
You must be signed in to change notification settings - Fork 45
Emit dial events rather than logging them #563
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
base: main
Are you sure you want to change the base?
Changes from all commits
3a8cc06
27aed38
e535dca
e5a8f9d
d01560e
636500c
462bb62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
type Callback = (args: unknown) => void; | ||
type Callback<T = unknown> = (args: T) => void; | ||
|
||
/** | ||
* MachineConnectionEvent events are emitted by a Client's EventDispatcher when | ||
|
@@ -9,21 +9,22 @@ export enum MachineConnectionEvent { | |
CONNECTED = 'connected', | ||
DISCONNECTING = 'disconnecting', | ||
DISCONNECTED = 'disconnected', | ||
DIAL_EVENT = 'dialing', | ||
} | ||
|
||
export class EventDispatcher { | ||
listeners: Partial<Record<string, Set<Callback>>> = {}; | ||
|
||
on(type: string, listener: Callback) { | ||
on<T>(type: string, listener: Callback<T>) { | ||
const { listeners } = this; | ||
listeners[type] ??= new Set(); | ||
listeners[type]?.add(listener); | ||
listeners[type]?.add(listener as Callback); | ||
} | ||
|
||
once(type: string, listener: Callback) { | ||
const fn = (args: unknown) => { | ||
once<T>(type: string, listener: Callback<T>) { | ||
const fn = (args: T) => { | ||
listener(args); | ||
this.off(type, listener); | ||
this.off(type, listener as Callback); | ||
}; | ||
this.on(type, fn); | ||
} | ||
|
@@ -36,7 +37,7 @@ export class EventDispatcher { | |
this.listeners[type]?.delete(listener); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why aren't we passing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, |
||
|
||
emit(type: string, args: unknown) { | ||
emit<T = unknown>(type: string, args: T) { | ||
for (const callback of this.listeners[type] ?? []) { | ||
callback(args); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -210,17 +210,18 @@ export class RobotClient extends EventDispatcher implements Robot { | |
return; | ||
} | ||
|
||
// eslint-disable-next-line no-console | ||
console.debug('Connection closed, will try to reconnect'); | ||
this.emit(MachineConnectionEvent.DIAL_EVENT, { | ||
message: 'Connection closed, attempting to reconnect', | ||
}); | ||
|
||
const backOffOpts: Partial<IBackOffOptions> = { | ||
retry: (error, attemptNumber) => { | ||
retry: (error: Error, attemptNumber) => { | ||
// TODO: This ought to check exceptional errors so as to not keep failing forever. | ||
|
||
// eslint-disable-next-line no-console | ||
console.debug( | ||
`Failed to connect, attempt ${attemptNumber} with backoff`, | ||
error | ||
); | ||
this.emit(MachineConnectionEvent.DIAL_EVENT, { | ||
message: `Failed to connect, attempt ${attemptNumber} with backoff`, | ||
error, | ||
}); | ||
|
||
// Always retry the next attempt | ||
return true; | ||
|
@@ -234,12 +235,14 @@ export class RobotClient extends EventDispatcher implements Robot { | |
} | ||
void backOff(async () => this.connect(), backOffOpts) | ||
.then(() => { | ||
// eslint-disable-next-line no-console | ||
console.debug('Reconnected successfully!'); | ||
this.emit(MachineConnectionEvent.DIAL_EVENT, { | ||
message: 'Reconnected successfully', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this a |
||
}); | ||
}) | ||
.catch(() => { | ||
// eslint-disable-next-line no-console | ||
console.debug(`Reached max attempts: ${this.reconnectMaxAttempts}`); | ||
this.emit(MachineConnectionEvent.DIAL_EVENT, { | ||
message: `Reached max attempts: ${this.reconnectMaxAttempts}`, | ||
}); | ||
}); | ||
} | ||
|
||
|
@@ -502,6 +505,7 @@ export class RobotClient extends EventDispatcher implements Robot { | |
} | ||
|
||
const webRTCConn = await dialWebRTC( | ||
this, | ||
this.webrtcOptions.signalingAddress || this.serviceHost, | ||
this.webrtcOptions.host, | ||
opts | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
import { backOff, type IBackOffOptions } from 'exponential-backoff'; | ||
import { backOff } from 'exponential-backoff'; | ||
import { isCredential } from '../app/viam-transport'; | ||
import { DIAL_TIMEOUT } from '../constants'; | ||
import type { AccessToken, Credential } from '../main'; | ||
import { | ||
MachineConnectionEvent, | ||
type AccessToken, | ||
type Credential, | ||
} from '../main'; | ||
import { RobotClient } from './client'; | ||
|
||
/** Options required to dial a robot via gRPC. */ | ||
|
@@ -27,9 +31,6 @@ const isPosInt = (x: number): boolean => { | |
const isLocalConnection = (url: string) => url.includes('local'); | ||
|
||
const dialDirect = async (conf: DialDirectConf): Promise<RobotClient> => { | ||
// eslint-disable-next-line no-console | ||
console.debug('dialing via gRPC...'); | ||
|
||
if (!isLocalConnection(conf.host)) { | ||
throw new Error( | ||
`cannot dial "${conf.host}" directly, please use a local url instead.` | ||
|
@@ -48,13 +49,18 @@ const dialDirect = async (conf: DialDirectConf): Promise<RobotClient> => { | |
} | ||
const client = new RobotClient(conf.host, undefined, sessOpts, clientConf); | ||
|
||
client.emit(MachineConnectionEvent.DIAL_EVENT, { | ||
message: 'dialing via gRPC', | ||
}); | ||
|
||
await client.connect({ | ||
creds: conf.credentials, | ||
dialTimeout: conf.dialTimeout ?? DIAL_TIMEOUT, | ||
}); | ||
|
||
// eslint-disable-next-line no-console | ||
console.debug('connected via gRPC'); | ||
client.emit(MachineConnectionEvent.DIAL_EVENT, { | ||
message: 'connected via gRPC', | ||
}); | ||
|
||
return client; | ||
}; | ||
|
@@ -92,9 +98,6 @@ export interface DialWebRTCConf { | |
} | ||
|
||
const dialWebRTC = async (conf: DialWebRTCConf): Promise<RobotClient> => { | ||
// eslint-disable-next-line no-console | ||
console.debug('dialing via WebRTC...'); | ||
|
||
const impliedURL = conf.serviceHost ?? conf.host; | ||
const { signalingAddress } = conf; | ||
const iceServers = conf.iceServers ?? []; | ||
|
@@ -115,14 +118,19 @@ const dialWebRTC = async (conf: DialWebRTCConf): Promise<RobotClient> => { | |
} | ||
const client = new RobotClient(impliedURL, clientConf, sessOpts); | ||
|
||
client.emit(MachineConnectionEvent.DIAL_EVENT, { | ||
message: 'Dialing via WebRTC.', | ||
}); | ||
|
||
await client.connect({ | ||
priority: conf.priority, | ||
dialTimeout: conf.dialTimeout ?? DIAL_TIMEOUT, | ||
creds: conf.credentials, | ||
}); | ||
|
||
// eslint-disable-next-line no-console | ||
console.debug('connected via WebRTC'); | ||
client.emit(MachineConnectionEvent.DIAL_EVENT, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similarly, why is this a |
||
message: 'Connected via WebRTC.', | ||
}); | ||
|
||
return client; | ||
}; | ||
|
@@ -160,33 +168,28 @@ export const createRobotClient = async ( | |
): Promise<RobotClient> => { | ||
validateDialConf(conf); | ||
|
||
const backOffOpts: Partial<IBackOffOptions> = { | ||
retry: (error, attemptNumber) => { | ||
// TODO: This ought to check exceptional errors so as to not keep failing forever. | ||
|
||
// eslint-disable-next-line no-console | ||
console.debug( | ||
`Failed to connect, attempt ${attemptNumber} with backoff`, | ||
error | ||
); | ||
|
||
// Abort reconnects if the the caller specifies, otherwise retry | ||
return !conf.reconnectAbortSignal?.abort; | ||
}, | ||
}; | ||
if (conf.reconnectMaxWait !== undefined) { | ||
backOffOpts.maxDelay = conf.reconnectMaxWait; | ||
} | ||
if (conf.reconnectMaxAttempts !== undefined) { | ||
backOffOpts.numOfAttempts = conf.reconnectMaxAttempts; | ||
} | ||
|
||
// Try to dial via WebRTC first. | ||
if (isDialWebRTCConf(conf) && !conf.reconnectAbortSignal?.abort) { | ||
try { | ||
return conf.noReconnect | ||
const client = conf.noReconnect | ||
? await dialWebRTC(conf) | ||
: await backOff(async () => dialWebRTC(conf), backOffOpts); | ||
: await backOff(async () => dialWebRTC(conf), { | ||
maxDelay: conf.reconnectMaxWait, | ||
numOfAttempts: conf.reconnectMaxAttempts, | ||
retry: (error: Error, attemptNumber) => { | ||
// TODO: This ought to check exceptional errors so as to not keep failing forever. | ||
|
||
client.emit(MachineConnectionEvent.DIAL_EVENT, { | ||
message: `Failed to connect via WebRTC, attempt ${attemptNumber} with backoff.`, | ||
error, | ||
}); | ||
|
||
// Abort reconnects if the the caller specifies, otherwise retry | ||
return !conf.reconnectAbortSignal?.abort; | ||
}, | ||
}); | ||
|
||
return client; | ||
} catch { | ||
// eslint-disable-next-line no-console | ||
console.debug('Failed to connect via WebRTC'); | ||
|
@@ -195,9 +198,25 @@ export const createRobotClient = async ( | |
|
||
if (!conf.reconnectAbortSignal?.abort) { | ||
try { | ||
return conf.noReconnect | ||
const client = conf.noReconnect | ||
? await dialDirect(conf) | ||
: await backOff(async () => dialDirect(conf), backOffOpts); | ||
: await backOff(async () => dialDirect(conf), { | ||
maxDelay: conf.reconnectMaxWait, | ||
numOfAttempts: conf.reconnectMaxAttempts, | ||
retry: (error: Error, attemptNumber) => { | ||
// TODO: This ought to check exceptional errors so as to not keep failing forever. | ||
|
||
client.emit(MachineConnectionEvent.DIAL_EVENT, { | ||
message: `Failed to connect via gRPC, attempt ${attemptNumber} with backoff.`, | ||
error, | ||
}); | ||
|
||
// Abort reconnects if the the caller specifies, otherwise retry | ||
return !conf.reconnectAbortSignal?.abort; | ||
}, | ||
}); | ||
|
||
return client; | ||
} catch { | ||
// eslint-disable-next-line no-console | ||
console.debug('Failed to connect via gRPC'); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
DIALING
different thanCONNECTING
?