Skip to content
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

fix(WebSocketShard): handle initial connect being a resume #9549

Merged
merged 3 commits into from
May 11, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions packages/ws/src/ws/WebSocketShard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,28 @@ export class WebSocketShard extends AsyncEventEmitter<WebSocketShardEventsMap> {
}

public async connect() {
const promise = this.initialConnectResolved ? Promise.resolve() : once(this, WebSocketShardEvents.Ready);
const controller = new AbortController();
let promise;

if (!this.initialConnectResolved) {
// Sleep for the remaining time, but if the connection closes in the meantime, we shouldn't wait the remainder to avoid blocking the new conn
promise = Promise.race([
once(this, WebSocketShardEvents.Ready, { signal: controller.signal }),
once(this, WebSocketShardEvents.Resumed, { signal: controller.signal }),
]);

controller.abort();
didinele marked this conversation as resolved.
Show resolved Hide resolved
}

void this.internalConnect();

await promise;
try {
await promise;
} finally {
// cleanup hanging listeners
controller.abort();
}

this.initialConnectResolved = true;
}

Expand Down