-
Notifications
You must be signed in to change notification settings - Fork 238
Typesafe errors in connection path #1763
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 1 commit
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 |
|---|---|---|
|
|
@@ -1079,7 +1079,7 @@ | |
| regionUrl?: string, | ||
| ): Promise<Result<void, UnexpectedConnectionState | SignalReconnectError>> { | ||
| const self = this; | ||
| const restartResultAsync = safeTry(async function* () { | ||
| if (!self.url || !self.token) { | ||
| // permanent failure, don't attempt reconnection | ||
| return err(new UnexpectedConnectionState('could not reconnect, url or token not saved')); | ||
|
|
@@ -1119,7 +1119,7 @@ | |
| self.client.setReconnected(); | ||
| self.emit(EngineEvent.SignalRestarted, joinResult.value); | ||
|
|
||
| await self.waitForPCReconnected(); | ||
| yield* await self.waitForPCReconnected(); | ||
|
|
||
| // re-check signal connection state before setting engine as resumed | ||
| if (self.client.currentState !== SignalConnectionState.CONNECTED) { | ||
|
|
@@ -1150,7 +1150,7 @@ | |
|
|
||
| private async resumeConnection( | ||
| reason?: ReconnectReason, | ||
| ): Promise<Result<void, SignalReconnectError>> { | ||
| ): Promise<Result<void, SignalReconnectError | UnexpectedConnectionState>> { | ||
| if (!this.url || !this.token) { | ||
| // permanent failure, don't attempt reconnection | ||
| return errAsync(new UnexpectedConnectionState('could not reconnect, url or token not saved')); | ||
|
|
@@ -1172,7 +1172,11 @@ | |
| ); | ||
|
|
||
| if (reconnectResult.isErr()) { | ||
| return err(reconnectResult.error); | ||
| return err( | ||
| new SignalReconnectError( | ||
|
Contributor
Author
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. one of the errors was caused by this method returning a This led to the reconnect flow triggering a "full reconnect" instead of a "resume" as "resumes" can only happen for SignalReconnectErrors |
||
| `${reconnectResult.error.reasonName}: ${reconnectResult.error.message}`, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| this.emit(EngineEvent.SignalResumed); | ||
|
|
@@ -1224,24 +1228,30 @@ | |
| if (!this.pcManager) { | ||
| throw new UnexpectedConnectionState('PC manager is closed'); | ||
| } | ||
| await this.pcManager.ensurePCTransportConnection(abortController, timeout); | ||
| return this.pcManager.ensurePCTransportConnection(abortController, timeout); | ||
| } | ||
|
|
||
| private async waitForPCReconnected() { | ||
| private async waitForPCReconnected(): Promise< | ||
| Result<void, UnexpectedConnectionState | ConnectionError> | ||
| > { | ||
| this.pcState = PCState.Reconnecting; | ||
|
|
||
| this.log.debug('waiting for peer connection to reconnect', this.logContext); | ||
| try { | ||
| await sleep(minReconnectWait); // FIXME setTimeout again not ideal for a connection critical path | ||
| if (!this.pcManager) { | ||
| throw new UnexpectedConnectionState('PC manager is closed'); | ||
| return err(new UnexpectedConnectionState('PC manager is closed')); | ||
| } | ||
| await this.pcManager.ensurePCTransportConnection(undefined, this.peerConnectionTimeout); | ||
| const res = await this.pcManager.ensurePCTransportConnection( | ||
| undefined, | ||
| this.peerConnectionTimeout, | ||
| ); | ||
| this.pcState = PCState.Connected; | ||
| return res; | ||
| } catch (e: any) { | ||
| // TODO do we need a `failed` state here for the PC? | ||
| this.pcState = PCState.Disconnected; | ||
| throw ConnectionError.internal(`could not establish PC connection: ${e.message}`); | ||
| return err(ConnectionError.internal(`could not establish PC connection: ${e.message}`)); | ||
| } | ||
| } | ||
|
|
||
|
|
||
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.
the other error was caused by self.waitForPCReconnected throwing.
This is the pitfall of doing a gradual adoption of this pattern. Extra care needed for ensuring that if a method is expected to not throw anymore it actually won't