Skip to content

Commit 6e5fdcb

Browse files
ronagRafaelGSS
andcommitted
http: optimize IncomingMessage._dump
Signed-off-by: RafaelGSS <[email protected]> Co-Authored-By: RafaelGSS <[email protected]>
1 parent 2ea31e5 commit 6e5fdcb

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

doc/api/http.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3537,6 +3537,9 @@ Found'`.
35373537
<!-- YAML
35383538
added: v0.1.13
35393539
changes:
3540+
- version: REPLACEME
3541+
pr-url: https://github.com/nodejs/node/pull/59778
3542+
description: Add fastDump option.
35403543
- version:
35413544
- v20.1.0
35423545
- v18.17.0
@@ -3632,6 +3635,11 @@ changes:
36323635
* `rejectNonStandardBodyWrites` {boolean} If set to `true`, an error is thrown
36333636
when writing to an HTTP response which does not have a body.
36343637
**Default:** `false`.
3638+
* `fastDump` {boolean} If set to `true`, request body for `HEAD` and `GET`
3639+
requests will be immediatly dumped and the `end` and `close` events for
3640+
`IncomingMessage` will be emitted before the server emits `'request'`. This
3641+
enables some optimizations that would otherwise not be possible.
3642+
**Default:** `false`.
36353643
36363644
* `requestListener` {Function}
36373645

lib/_http_server.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ const onResponseFinishChannel = dc.channel('http.server.response.finish');
106106
const kServerResponse = Symbol('ServerResponse');
107107
const kServerResponseStatistics = Symbol('ServerResponseStatistics');
108108

109+
const kfastDump = Symbol('FastDumpOption');
110+
109111
const {
110112
hasObserver,
111113
startPerf,
@@ -452,6 +454,11 @@ function storeHTTPOptions(options) {
452454
validateInteger(maxHeaderSize, 'maxHeaderSize', 0);
453455
this.maxHeaderSize = maxHeaderSize;
454456

457+
const fastDump = options.fastDump;
458+
if (fastDump !== undefined)
459+
validateBoolean(fastDump, 'options.fastDump');
460+
this[kfastDump] = fastDump || false;
461+
455462
const insecureHTTPParser = options.insecureHTTPParser;
456463
if (insecureHTTPParser !== undefined)
457464
validateBoolean(insecureHTTPParser, 'options.insecureHTTPParser');
@@ -1102,6 +1109,23 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
11021109
});
11031110
}
11041111

1112+
if (req[kfastDump] === true && (req.method === 'HEAD' || req.method === 'GET')) {
1113+
// Fast dump where request "has" already emitted all lifecycle events.
1114+
// This avoid a lot of unnecessary overhead otherwise introduced by
1115+
// stream.Readable life cycle rules. The downside is that this will
1116+
// break some servers that read GET bodies.
1117+
1118+
req._dumped = true;
1119+
// TODO: move that to lazy_transform.js
1120+
req._readableState.ended = true;
1121+
req._readableState.endEmitted = true;
1122+
req._readableState.destroyed = true;
1123+
req._readableState.closed = true;
1124+
req._readableState.closeEmitted = true;
1125+
1126+
req._read();
1127+
}
1128+
11051129
if (socket._httpMessage) {
11061130
// There are already pending outgoing res, append.
11071131
state.outgoing.push(res);

test/parallel/test-http-chunk-extensions-limit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const assert = require('assert');
124124
}));
125125

126126
sock.end('' +
127-
'GET / HTTP/1.1\r\n' +
127+
'PUT / HTTP/1.1\r\n' +
128128
`Host: localhost:${port}\r\n` +
129129
'Transfer-Encoding: chunked\r\n\r\n' +
130130
'2;' + 'A'.repeat(10000) + '=bar\r\nAA\r\n' +

test/parallel/test-http.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,18 @@ const server = http.Server(common.mustCall((req, res) => {
5252
if (expectedRequests.length === 0)
5353
server.close();
5454

55-
req.on('end', () => {
55+
if (req.readableEnded) {
5656
res.writeHead(200, { 'Content-Type': 'text/plain' });
5757
res.write(`The path was ${url.parse(req.url).pathname}`);
5858
res.end();
59-
});
60-
req.resume();
59+
} else {
60+
req.on('end', () => {
61+
res.writeHead(200, { 'Content-Type': 'text/plain' });
62+
res.write(`The path was ${url.parse(req.url).pathname}`);
63+
res.end();
64+
});
65+
req.resume();
66+
}
6167
}, 3));
6268
server.listen(0);
6369

0 commit comments

Comments
 (0)