Skip to content

Commit ad5306a

Browse files
katreniakBranislav Katreniak
and
Branislav Katreniak
authored
perf(websocket): fix write back-pressure (#618)
This change should reduce memory usage when many packets are emitted to many clients in a burst. Co-authored-by: Branislav Katreniak <[email protected]>
1 parent 4c0aa73 commit ad5306a

File tree

2 files changed

+27
-23
lines changed

2 files changed

+27
-23
lines changed

lib/transports/websocket.js

+24-22
Original file line numberDiff line numberDiff line change
@@ -63,33 +63,35 @@ class WebSocket extends Transport {
6363
* @api private
6464
*/
6565
send(packets) {
66-
for (let i = 0; i < packets.length; i++) {
67-
const packet = packets[i];
66+
const packet = packets.shift();
67+
if (typeof packet === "undefined") {
68+
this.writable = true;
69+
this.emit("drain");
70+
return;
71+
}
6872

69-
// always creates a new object since ws modifies it
70-
const opts = {};
71-
if (packet.options) {
72-
opts.compress = packet.options.compress;
73-
}
73+
// always creates a new object since ws modifies it
74+
const opts = {};
75+
if (packet.options) {
76+
opts.compress = packet.options.compress;
77+
}
7478

75-
this.parser.encodePacket(packet, this.supportsBinary, data => {
76-
if (this.perMessageDeflate) {
77-
const len =
78-
"string" === typeof data ? Buffer.byteLength(data) : data.length;
79-
if (len < this.perMessageDeflate.threshold) {
80-
opts.compress = false;
81-
}
79+
this.parser.encodePacket(packet, this.supportsBinary, data => {
80+
if (this.perMessageDeflate) {
81+
const len =
82+
"string" === typeof data ? Buffer.byteLength(data) : data.length;
83+
if (len < this.perMessageDeflate.threshold) {
84+
opts.compress = false;
8285
}
83-
debug('writing "%s"', data);
84-
this.writable = false;
86+
}
87+
debug('writing "%s"', data);
88+
this.writable = false;
8589

86-
this.socket.send(data, opts, err => {
87-
if (err) return this.onError("write error", err.stack);
88-
this.writable = true;
89-
this.emit("drain");
90-
});
90+
this.socket.send(data, opts, err => {
91+
if (err) return this.onError("write error", err.stack);
92+
this.send(packets);
9193
});
92-
}
94+
});
9395
}
9496

9597
/**

test/server.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,9 @@ describe("server", () => {
16811681
conn.send("a");
16821682
conn.send("b");
16831683
conn.send("c");
1684-
conn.close();
1684+
setTimeout(() => {
1685+
conn.close();
1686+
}, 50);
16851687
});
16861688

16871689
socket.on("open", () => {

0 commit comments

Comments
 (0)