src: do not reuse async resource in http parsers

Change resource being used, previously HTTParser was being reused.
We are now using IncomingMessage and ClientRequest objects.  The goal
here is to make the async resource unique for each async operatio

Refs: https://github.com/nodejs/node/pull/24330
Refs: https://github.com/nodejs/diagnostics/issues/248
Refs: https://github.com/nodejs/node/pull/21313

Co-authored-by: Matheus Marchini <mat@mmarchini.me>

PR-URL: https://github.com/nodejs/node/pull/25094
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benedikt Meurer <benedikt.meurer@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Daniel BeckertandMatheus Marchini authored and Matheus Marchini committed 2019-04-22 11:51:46 -07:00
1 parent 5aaf666b3b
commit ece507394a
13 files changed
+102 -32

No files matched your search

+39
View File
@@ -0,0 +1,39 @@
'use strict';
const common = require('../common');
const http = require('http');
const assert = require('assert');
const { createHook } = require('async_hooks');
const reused = Symbol('reused');
let reusedHTTPParser = false;
const asyncHook = createHook({
init(asyncId, type, triggerAsyncId, resource) {
if (resource[reused]) {
reusedHTTPParser = true;
}
resource[reused] = true;
}
});
asyncHook.enable();
const server = http.createServer(function(req, res) {
res.end();
});
const PORT = 3000;
const url = 'http://127.0.0.1:' + PORT;
server.listen(PORT, common.mustCall(() => {
http.get(url, common.mustCall(() => {
server.close(common.mustCall(() => {
server.listen(PORT, common.mustCall(() => {
http.get(url, common.mustCall(() => {
server.close(common.mustCall(() => {
assert.strictEqual(reusedHTTPParser, false);
}));
}));
}));
}));
}));
}));
+2 -2
View File
@@ -21,7 +21,7 @@ const request = Buffer.from(
);
const parser = new HTTPParser(REQUEST);
const as = hooks.activitiesOfTypes('HTTPPARSER');
const as = hooks.activitiesOfTypes('HTTPINCOMINGMESSAGE');
const httpparser = as[0];
assert.strictEqual(as.length, 1);
@@ -47,7 +47,7 @@ process.on('exit', onexit);
function onexit() {
hooks.disable();
hooks.sanityCheck('HTTPPARSER');
hooks.sanityCheck('HTTPINCOMINGMESSAGE');
checkInvocations(httpparser, { init: 1, before: 1, after: 1, destroy: 1 },
'when process exits');
}
+2 -2
View File
@@ -26,7 +26,7 @@ const request = Buffer.from(
);
const parser = new HTTPParser(RESPONSE);
const as = hooks.activitiesOfTypes('HTTPPARSER');
const as = hooks.activitiesOfTypes('HTTPCLIENTREQUEST');
const httpparser = as[0];
assert.strictEqual(as.length, 1);
@@ -58,7 +58,7 @@ process.on('exit', onexit);
function onexit() {
hooks.disable();
hooks.sanityCheck('HTTPPARSER');
hooks.sanityCheck('HTTPCLIENTREQUEST');
checkInvocations(httpparser, { init: 1, before: 2, after: 2, destroy: 1 },
'when process exits');
}
+2 -2
View File
@@ -95,7 +95,7 @@ function expectBody(expected) {
throw new Error('hello world');
};
parser.reinitialize(HTTPParser.REQUEST, false);
parser.initialize(HTTPParser.REQUEST, request);
assert.throws(
() => { parser.execute(request, 0, request.length); },
@@ -555,7 +555,7 @@ function expectBody(expected) {
parser[kOnBody] = expectBody('ping');
parser.execute(req1, 0, req1.length);
parser.reinitialize(REQUEST, false);
parser.initialize(REQUEST, req2);
parser[kOnBody] = expectBody('pong');
parser[kOnHeadersComplete] = onHeadersComplete2;
parser.execute(req2, 0, req2.length);
@@ -48,6 +48,8 @@ const { getSystemErrorName } = require('util');
if (!common.isMainThread)
delete providers.INSPECTORJSBINDING;
delete providers.KEYPAIRGENREQUEST;
delete providers.HTTPCLIENTREQUEST;
delete providers.HTTPINCOMINGMESSAGE;
const objKeys = Object.keys(providers);
if (objKeys.length > 0)
+2 -1
View File
@@ -7,6 +7,7 @@ const common = require('../common');
const assert = require('assert');
const httpCommon = require('_http_common');
const { HTTPParser } = require('_http_common');
const { AsyncResource } = require('async_hooks');
const net = require('net');
const COUNT = httpCommon.parsers.max + 1;
@@ -24,7 +25,7 @@ function execAndClose() {
process.stdout.write('.');
const parser = parsers.pop();
parser.reinitialize(HTTPParser.RESPONSE, !!parser.reused);
parser.initialize(HTTPParser.RESPONSE, new AsyncResource('ClientRequest'));
const socket = net.connect(common.PORT);
socket.on('error', (e) => {