Skip to content

Commit

Permalink
fix: do not drain the queue while the socket is offline
Browse files Browse the repository at this point in the history
In the previous implementation added in [1], the socket would try to
send the packet even if it was disconnected, which would needlessly
exhaust the number of retries.

[1]: 655dce9
  • Loading branch information
darrachequesne committed Feb 20, 2023
1 parent 5980918 commit 4996f9e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,15 +543,17 @@ export class Socket<

/**
* Send the first packet of the queue, and wait for an acknowledgement from the server.
* @param force - whether to resend a packet that has not been acknowledged yet
*
* @private
*/
private _drainQueue() {
private _drainQueue(force = false) {
debug("draining queue");
if (this._queue.length === 0) {
if (!this.connected || this._queue.length === 0) {
return;
}
const packet = this._queue[0];
if (packet.pending) {
if (packet.pending && !force) {
debug(
"packet [%d] has already been sent and is waiting for an ack",
packet.id
Expand Down Expand Up @@ -776,6 +778,7 @@ export class Socket<
this.connected = true;
this.emitBuffered();
this.emitReserved("connect");
this._drainQueue(true);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions test/retry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,25 @@ describe("retry", () => {
});
});
});

it("should not drain the queue while the socket is disconnected", () => {
return wrap((done) => {
const socket = io(BASE_URL, {
forceNew: true,
autoConnect: false,
retries: 3,
ackTimeout: 10,
});

socket.emit("echo", 1, (err) => {
expect(err).to.be(null);

success(done, socket);
});

setTimeout(() => {
socket.connect();
}, 100);
});
});
});

0 comments on commit 4996f9e

Please sign in to comment.