Skip to content

Commit

Permalink
fix(WebSocketShard): handle initial connect being a resume (#9549)
Browse files Browse the repository at this point in the history
* fix(WebSocketShard): handle initial connect being a resume

* chore: remove leftover

---------

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
didinele and kodiakhq[bot] committed May 11, 2023
1 parent a51c48e commit 4dcc9c5
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/ws/src/ws/WebSocketShard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,26 @@ 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 }),
]);
}

void this.internalConnect();

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

this.initialConnectResolved = true;
}

Expand Down

0 comments on commit 4dcc9c5

Please sign in to comment.