-
Notifications
You must be signed in to change notification settings - Fork 47
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3a8cc06
add events
micheal-parks 27aed38
Merge branch 'main' into events-1
micheal-parks e535dca
more events
micheal-parks e5a8f9d
emit more events
micheal-parks d01560e
Cleanup
micheal-parks 636500c
typecheck
micheal-parks 462bb62
make once generic
micheal-parks 7cebc3b
Merge branch 'main' of https://github.com/micheal-parks/viam-typescri…
micheal-parks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
?