Skip to content

Commit

Permalink
perf(websocket): fix write back-pressure (#618)
Browse files Browse the repository at this point in the history
This change should reduce memory usage when many packets are emitted to
many clients in a burst.

Co-authored-by: Branislav Katreniak <bkatreniak@slido.com>
  • Loading branch information
katreniak and Branislav Katreniak authored May 4, 2021
1 parent 4c0aa73 commit ad5306a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
46 changes: 24 additions & 22 deletions lib/transports/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,33 +63,35 @@ class WebSocket extends Transport {
* @api private
*/
send(packets) {
for (let i = 0; i < packets.length; i++) {
const packet = packets[i];
const packet = packets.shift();
if (typeof packet === "undefined") {
this.writable = true;
this.emit("drain");
return;
}

// always creates a new object since ws modifies it
const opts = {};
if (packet.options) {
opts.compress = packet.options.compress;
}
// always creates a new object since ws modifies it
const opts = {};
if (packet.options) {
opts.compress = packet.options.compress;
}

this.parser.encodePacket(packet, this.supportsBinary, data => {
if (this.perMessageDeflate) {
const len =
"string" === typeof data ? Buffer.byteLength(data) : data.length;
if (len < this.perMessageDeflate.threshold) {
opts.compress = false;
}
this.parser.encodePacket(packet, this.supportsBinary, data => {
if (this.perMessageDeflate) {
const len =
"string" === typeof data ? Buffer.byteLength(data) : data.length;
if (len < this.perMessageDeflate.threshold) {
opts.compress = false;
}
debug('writing "%s"', data);
this.writable = false;
}
debug('writing "%s"', data);
this.writable = false;

this.socket.send(data, opts, err => {
if (err) return this.onError("write error", err.stack);
this.writable = true;
this.emit("drain");
});
this.socket.send(data, opts, err => {
if (err) return this.onError("write error", err.stack);
this.send(packets);
});
}
});
}

/**
Expand Down
4 changes: 3 additions & 1 deletion test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1681,7 +1681,9 @@ describe("server", () => {
conn.send("a");
conn.send("b");
conn.send("c");
conn.close();
setTimeout(() => {
conn.close();
}, 50);
});

socket.on("open", () => {
Expand Down

0 comments on commit ad5306a

Please # to comment.