Skip to content

Commit a382c1c

Browse files
GetThatCookietrivikr
authored andcommitted
http: coalesce chunked writes during auto-corking
Signed-off-by: GetThatCookie <NimmenKeks@gmx.de> PR-URL: #64987 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Tim Perry <pimterry@gmail.com>
1 parent 2cb8064 commit a382c1c

6 files changed

Lines changed: 89 additions & 46 deletions

File tree

‎lib/_http_outgoing.js‎

Lines changed: 70 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const {
6767
ERR_STREAM_DESTROYED,
6868
ERR_STREAM_NULL_VALUES,
6969
ERR_STREAM_WRITE_AFTER_END,
70+
ERR_UNKNOWN_ENCODING,
7071
},
7172
hideStackFrames,
7273
} = require('internal/errors');
@@ -230,7 +231,12 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'writableObjectMode', {
230231
ObjectDefineProperty(OutgoingMessage.prototype, 'writableLength', {
231232
__proto__: null,
232233
get() {
233-
return this.outputSize + this[kChunkedLength] + (this[kSocket] ? this[kSocket].writableLength : 0);
234+
let bufferedLength = this[kChunkedLength];
235+
if (bufferedLength !== 0) {
236+
bufferedLength += bufferedLength.toString(16).length + 4 +
237+
(!this._headerSent && this._header !== null ? this._header.length : 0);
238+
}
239+
return this.outputSize + bufferedLength + (this[kSocket] ? this[kSocket].writableLength : 0);
234240
},
235241
});
236242

@@ -301,37 +307,45 @@ OutgoingMessage.prototype.cork = function cork() {
301307

302308
OutgoingMessage.prototype.uncork = function uncork() {
303309
this[kCorked]--;
304-
if (this[kSocket]) {
305-
this[kSocket].uncork();
306-
}
307-
308-
if (this[kCorked] || this[kChunkedBuffer].length === 0) {
309-
return;
310-
}
311-
312-
const len = this[kChunkedLength];
313-
const buf = this[kChunkedBuffer];
310+
const socket = this[kSocket];
311+
try {
312+
if (this[kCorked] || this[kChunkedBuffer].length === 0) {
313+
return;
314+
}
314315

315-
assert(this.chunkedEncoding);
316+
const len = this[kChunkedLength];
317+
const buf = this[kChunkedBuffer];
318+
if (this.destroyed || socket?.destroyed) {
319+
const error = this[kErrored] || socket?._writableState?.errored || new ERR_STREAM_DESTROYED('write');
320+
for (let n = 2; n < buf.length; n += 3) {
321+
if (buf[n] !== nop) {
322+
process.nextTick(buf[n], error);
323+
}
324+
}
325+
buf.length = 0;
326+
this[kChunkedLength] = 0;
327+
return;
328+
}
329+
assert(this.chunkedEncoding);
316330

317-
let callbacks;
318-
this._send(len.toString(16), 'latin1', null);
319-
this._send(crlf_buf, null, null);
320-
for (let n = 0; n < buf.length; n += 3) {
321-
this._send(buf[n + 0], buf[n + 1], null);
322-
if (buf[n + 2]) {
323-
callbacks ??= [];
331+
const callbacks = [];
332+
this._send(len.toString(16), 'latin1', null);
333+
this._send(crlf_buf, null, null);
334+
for (let n = 0; n < buf.length; n += 3) {
335+
this._send(buf[n + 0], buf[n + 1], null);
324336
callbacks.push(buf[n + 2]);
325337
}
326-
}
327-
this._send(crlf_buf, null, callbacks.length ? (err) => {
328-
for (const callback of callbacks) {
329-
callback(err);
330-
}
331-
} : null);
338+
this._send(crlf_buf, null, (err) => {
339+
for (const callback of callbacks) {
340+
callback(err);
341+
}
342+
});
332343

333-
this[kChunkedBuffer].length = 0;
334-
this[kChunkedLength] = 0;
344+
this[kChunkedBuffer].length = 0;
345+
this[kChunkedLength] = 0;
346+
} finally {
347+
socket?.uncork();
348+
}
335349

336350
// If we had a pending drain and flushed all data, emit the drain event.
337351
if (this[kNeedDrain] && this.writableLength === 0) {
@@ -367,6 +381,10 @@ OutgoingMessage.prototype.destroy = function destroy(error) {
367381
this.destroyed = true;
368382

369383
this[kErrored] = error;
384+
if (this[kChunkedBuffer].length !== 0) {
385+
this[kCorked] = 1;
386+
this.uncork();
387+
}
370388

371389
if (this[kSocket]) {
372390
this[kSocket].destroy(error);
@@ -1006,24 +1024,34 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
10061024
}
10071025
}
10081026

1027+
if (msg.chunkedEncoding && typeof chunk === 'string' && encoding && !Buffer.isEncoding(encoding)) {
1028+
throw new ERR_UNKNOWN_ENCODING(encoding);
1029+
}
1030+
10091031
if (!fromEnd && msg.socket && !msg.socket.writableCorked) {
1010-
msg.socket.cork();
1011-
process.nextTick(connectionCorkNT, msg.socket);
1032+
msg.cork();
1033+
process.nextTick(connectionCorkNT, msg);
10121034
}
10131035

10141036
let ret;
10151037
if (msg.chunkedEncoding && chunk.length !== 0) {
10161038
len ??= typeof chunk === 'string' ? Buffer.byteLength(chunk, encoding) : chunk.byteLength;
1017-
if (msg[kCorked] && msg._headerSent) {
1039+
if (msg[kCorked]) {
1040+
if (typeof chunk !== 'string' && !(chunk instanceof Buffer)) {
1041+
chunk = Stream._uint8ArrayToBuffer(chunk);
1042+
}
10181043
msg[kChunkedBuffer].push(chunk, encoding, callback);
10191044
msg[kChunkedLength] += len;
1020-
ret = msg[kChunkedLength] < msg[kHighWaterMark];
1045+
ret = msg.writableLength < msg.writableHighWaterMark;
10211046
} else {
10221047
msg._send(len.toString(16), 'latin1', null);
10231048
msg._send(crlf_buf, null, null);
10241049
msg._send(chunk, encoding, null, len);
10251050
ret = msg._send(crlf_buf, null, callback);
10261051
}
1052+
} else if (msg.chunkedEncoding && msg[kCorked] && msg[kChunkedBuffer].length !== 0) {
1053+
msg[kChunkedBuffer].push(chunk, encoding, callback);
1054+
ret = msg.writableLength < msg.writableHighWaterMark;
10271055
} else {
10281056
ret = msg._send(chunk, encoding, callback, len);
10291057
}
@@ -1052,8 +1080,10 @@ function maybePrepareFinalChunk(msg, chunk, encoding) {
10521080
return !!msg._header && msg._hasBody && !msg.chunkedEncoding;
10531081
}
10541082

1055-
function connectionCorkNT(conn) {
1056-
conn.uncork();
1083+
function connectionCorkNT(msg) {
1084+
if (msg[kCorked]) {
1085+
msg.uncork();
1086+
}
10571087
}
10581088

10591089
OutgoingMessage.prototype.addTrailers = function addTrailers(headers) {
@@ -1208,6 +1238,13 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
12081238
throw new ERR_HTTP_CONTENT_LENGTH_MISMATCH(this[kBytesWritten], this._contentLength);
12091239
}
12101240

1241+
// Flush buffered chunks before terminating the response.
1242+
if (this[kChunkedBuffer].length !== 0) {
1243+
this[kSocket]?.cork();
1244+
this[kCorked] = 1;
1245+
this.uncork();
1246+
}
1247+
12111248
if (finishCallback === null) {
12121249
// If we didn't early finish, send the last data and schedule 'finish' now:
12131250
finishCallback = onFinish.bind(undefined, this);

‎test/parallel/test-http-1.0.js‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,12 @@ function test(handler, request_generator, response_validator) {
127127
assert.strictEqual(req.httpVersionMinor, 1);
128128
res.sendDate = false;
129129
res.writeHead(200, { 'Content-Type': 'text/plain' });
130-
res.write('Hello, '); res._send('');
130+
assert.throws(() => res.write('ignored', 'invalid'), {
131+
code: 'ERR_UNKNOWN_ENCODING',
132+
});
133+
res.write('Hello, ', common.mustCall()); res._send('');
131134
res.write('world!'); res._send('');
132-
res.end();
135+
process.nextTick(() => res.end('X'));
133136
}
134137

135138
function request_generator() {
@@ -148,10 +151,10 @@ function test(handler, request_generator, response_validator) {
148151
'Connection: close\r\n' +
149152
'Transfer-Encoding: chunked\r\n' +
150153
'\r\n' +
151-
'7\r\n' +
152-
'Hello, \r\n' +
153-
'6\r\n' +
154-
'world!\r\n' +
154+
'd\r\n' +
155+
'Hello, world!\r\n' +
156+
'1\r\n' +
157+
'X\r\n' +
155158
'0\r\n' +
156159
'\r\n';
157160

‎test/parallel/test-http-byteswritten.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ const httpServer = http.createServer(common.mustCall(function(req, res) {
4444
res.write(bchunk);
4545
res.write(chunk, 'hex');
4646
}
47-
// Get .bytesWritten while buffer is not empty
48-
assert(res.connection.bytesWritten > 0);
49-
5047
res.end(body);
48+
49+
// Get .bytesWritten while the socket buffer is not empty.
50+
assert(res.connection.bytesWritten > 0);
5151
}));
5252

5353
httpServer.listen(0, function() {

‎test/parallel/test-http-outgoing-destroyed.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ const { OutgoingMessage } = require('http');
6666
});
6767
}));
6868
const err = new Error('Destroy test');
69+
res.write('x', common.mustCall((writeErr) => {
70+
assert.strictEqual(writeErr, err);
71+
}));
6972
res.destroy(err);
7073
assert.strictEqual(res.errored, err);
7174
})).listen(0, common.mustCall(() => {

‎test/parallel/test-stream-pipeline.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,12 @@ tmpdir.refresh();
243243
pipeline(rs, res, () => {});
244244
}));
245245

246-
let cnt = 10;
246+
let received = 0;
247247

248248
const badSink = new Writable({
249249
write(data, enc, cb) {
250-
cnt--;
251-
if (cnt === 0) cb(new Error('kaboom'));
250+
received += data.length;
251+
if (received >= 50) cb(new Error('kaboom'));
252252
else cb();
253253
}
254254
});

‎test/parallel/test-webstreams-pipeline.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ const http = require('http');
212212
values.push(chunk?.toString());
213213
});
214214
res.on('end', common.mustCall(() => {
215-
assert.deepStrictEqual(values, ['hello', 'world']);
215+
assert.strictEqual(values.join(''), 'helloworld');
216216
server.close();
217217
}));
218218
}));

0 commit comments

Comments
 (0)