Skip to content

Commit

Permalink
Don't treat PC config error as SignalReconnectError (#1052)
Browse files Browse the repository at this point in the history
* Don't treat PC config error as SignalReconnectError

* whitespace

* Create fast-goats-notice.md

* wait a bit for reconnect response to arrive

* remove waiting for reconnect response

* fix comment
  • Loading branch information
lukasIO authored Mar 1, 2024
1 parent de4a678 commit 81ab7db
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-goats-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

Don't treat PC config error as SignalReconnectError
14 changes: 9 additions & 5 deletions src/api/SignalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export class SignalClient {
token: string,
sid?: string,
reason?: ReconnectReason,
): Promise<ReconnectResponse | void> {
): Promise<ReconnectResponse | undefined> {
if (!this.options) {
this.log.warn(
'attempted to reconnect without signal options being set, ignoring',
Expand All @@ -242,7 +242,7 @@ export class SignalClient {
token: string,
opts: ConnectOpts,
abortSignal?: AbortSignal,
): Promise<JoinResponse | ReconnectResponse | void> {
): Promise<JoinResponse | ReconnectResponse | undefined> {
this.connectOptions = opts;
url = toWebsocketUrl(url);
// strip trailing slash
Expand All @@ -252,7 +252,7 @@ export class SignalClient {
const clientInfo = getClientInfo();
const params = createConnectionParams(token, clientInfo, opts);

return new Promise<JoinResponse | ReconnectResponse | void>(async (resolve, reject) => {
return new Promise<JoinResponse | ReconnectResponse | undefined>(async (resolve, reject) => {
const unlock = await this.connectionLock.lock();
try {
const abortHandler = async () => {
Expand Down Expand Up @@ -356,9 +356,13 @@ export class SignalClient {
abortSignal?.removeEventListener('abort', abortHandler);
this.startPingInterval();
if (resp.message?.case === 'reconnect') {
resolve(resp.message?.value);
resolve(resp.message.value);
} else {
resolve();
this.log.debug(
'declaring signal reconnected without reconnect response received',
this.logContext,
);
resolve(undefined);
shouldProcessMessage = true;
}
} else if (this.isEstablishingConnection && resp.message.case === 'leave') {
Expand Down
26 changes: 20 additions & 6 deletions src/room/RTCEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const lossyDataChannel = '_lossy';
const reliableDataChannel = '_reliable';
const minReconnectWait = 2 * 1000;
const leaveReconnect = 'leave-reconnect';
const maxReconnectResponseWait = 2 * 1000;

enum PCState {
New,
Expand Down Expand Up @@ -1005,14 +1006,10 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit

this.log.info(`resuming signal connection, attempt ${this.reconnectAttempts}`, this.logContext);
this.emit(EngineEvent.Resuming);

let res: ReconnectResponse | undefined;
try {
this.setupSignalClientCallbacks();
const res = await this.client.reconnect(this.url, this.token, this.participantSid, reason);
if (res) {
const rtcConfig = this.makeRTCConfiguration(res);
this.pcManager.updateConfiguration(rtcConfig);
}
res = await this.client.reconnect(this.url, this.token, this.participantSid, reason);
} catch (error) {
let message = '';
if (error instanceof Error) {
Expand All @@ -1029,6 +1026,23 @@ export default class RTCEngine extends (EventEmitter as new () => TypedEventEmit
}
this.emit(EngineEvent.SignalResumed);

if (!res) {
const startTime = Date.now();
while (Date.now() < startTime + maxReconnectResponseWait) {
if (res) {
break;
}
await sleep(50);
}
}

if (res) {
const rtcConfig = this.makeRTCConfiguration(res);
this.pcManager.updateConfiguration(rtcConfig);
} else {
this.log.warn('Did not receive reconnect response', this.logContext);
}

if (this.shouldFailNext) {
this.shouldFailNext = false;
throw new Error('simulated failure');
Expand Down
6 changes: 5 additions & 1 deletion src/room/track/LocalVideoTrack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,11 @@ async function setPublishingLayersForSender(
}

if (encodings.length !== senderEncodings.length) {
log.warn('cannot set publishing layers, encodings mismatch');
log.warn('cannot set publishing layers, encodings mismatch', {
...logContext,
encodings,
senderEncodings,
});
return;
}

Expand Down

0 comments on commit 81ab7db

Please sign in to comment.