Skip to content

Commit

Permalink
Merge pull request #2625 from threefoldtech/development_rmb_connection
Browse files Browse the repository at this point in the history
Wait for the opening connection in rmb client before closing it
  • Loading branch information
xmonader authored May 2, 2024
2 parents d2e641f + b012532 commit 34ea1c7
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/rmb_direct_client/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,17 @@ class Client {
}
}

private dropPingEnvelope() {
for (const id of this.responses.keys()) {
const envelope = this.responses.get(id);
if (envelope?.has_ping) this.responses.delete(id);
}
}

private async waitForResponses(timeoutInSeconds = 2 * 60): Promise<void> {
const start = new Date().getTime();
while (new Date().getTime() < start + timeoutInSeconds * 1000) {
this.dropPingEnvelope();
if (this.responses.size === 0) return;
this.logPendingResponses();
await new Promise(f => setTimeout(f, 1000));
Expand All @@ -196,6 +204,7 @@ class Client {
}

async disconnect() {
if (this.con.readyState === this.con.CONNECTING) await this.waitForOpenConnection();
if (this.__pingPongTimeout) clearTimeout(this.__pingPongTimeout);
this.con.removeAllListeners();
await this.waitForResponses();
Expand Down

0 comments on commit 34ea1c7

Please sign in to comment.