mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
async_hooks,http: set HTTPParser trigger to socket
This allows more easy tracking of where HTTP requests come from. Before this change the HTTPParser would have the HTTPServer as the triggerAsyncId. The HTTPParser will still have the executionAsyncId set to the HTTP Server so that information is still directly available. Indirectly, the TCP socket itself also has its triggerAsyncId set to the TCP Server. Backport-PR-URL: https://github.com/nodejs/node/pull/18179 PR-URL: https://github.com/nodejs/node/pull/18003 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Daijiro Wachi <daijiro.wachi@gmail.com>
This commit is contained in:
1 parent
5dab90b8bb
commit
91d4eb5ff8
3 files changed
+86
-10
No files matched your search
+19
-9
@@ -37,6 +37,10 @@ const {
|
||||
} = require('_http_common');
|
||||
const { OutgoingMessage } = require('_http_outgoing');
|
||||
const { outHeadersKey, ondrain } = require('internal/http');
|
||||
const {
|
||||
defaultTriggerAsyncIdScope,
|
||||
getOrSetAsyncId
|
||||
} = require('internal/async_hooks');
|
||||
|
||||
const STATUS_CODES = {
|
||||
100: 'Continue',
|
||||
@@ -289,6 +293,12 @@ Server.prototype.setTimeout = function setTimeout(msecs, callback) {
|
||||
|
||||
|
||||
function connectionListener(socket) {
|
||||
defaultTriggerAsyncIdScope(
|
||||
getOrSetAsyncId(socket), connectionListenerInternal, this, socket
|
||||
);
|
||||
}
|
||||
|
||||
function connectionListenerInternal(server, socket) {
|
||||
debug('SERVER new http connection');
|
||||
|
||||
httpSocketSetup(socket);
|
||||
@@ -296,13 +306,13 @@ function connectionListener(socket) {
|
||||
// Ensure that the server property of the socket is correctly set.
|
||||
// See https://github.com/nodejs/node/issues/13435
|
||||
if (socket.server === null)
|
||||
socket.server = this;
|
||||
socket.server = server;
|
||||
|
||||
// If the user has added a listener to the server,
|
||||
// request, or response, then it's their responsibility.
|
||||
// otherwise, destroy on timeout by default
|
||||
if (this.timeout && typeof socket.setTimeout === 'function')
|
||||
socket.setTimeout(this.timeout);
|
||||
if (server.timeout && typeof socket.setTimeout === 'function')
|
||||
socket.setTimeout(server.timeout);
|
||||
socket.on('timeout', socketOnTimeout);
|
||||
|
||||
var parser = parsers.alloc();
|
||||
@@ -312,8 +322,8 @@ function connectionListener(socket) {
|
||||
parser.incoming = null;
|
||||
|
||||
// Propagate headers limit from server instance to parser
|
||||
if (typeof this.maxHeadersCount === 'number') {
|
||||
parser.maxHeaderPairs = this.maxHeadersCount << 1;
|
||||
if (typeof server.maxHeadersCount === 'number') {
|
||||
parser.maxHeaderPairs = server.maxHeadersCount << 1;
|
||||
} else {
|
||||
// Set default value because parser may be reused from FreeList
|
||||
parser.maxHeaderPairs = 2000;
|
||||
@@ -333,8 +343,8 @@ function connectionListener(socket) {
|
||||
outgoingData: 0,
|
||||
keepAliveTimeoutSet: false
|
||||
};
|
||||
state.onData = socketOnData.bind(undefined, this, socket, parser, state);
|
||||
state.onEnd = socketOnEnd.bind(undefined, this, socket, parser, state);
|
||||
state.onData = socketOnData.bind(undefined, server, socket, parser, state);
|
||||
state.onEnd = socketOnEnd.bind(undefined, server, socket, parser, state);
|
||||
state.onClose = socketOnClose.bind(undefined, socket, state);
|
||||
state.onDrain = socketOnDrain.bind(undefined, socket, state);
|
||||
socket.on('data', state.onData);
|
||||
@@ -342,7 +352,7 @@ function connectionListener(socket) {
|
||||
socket.on('end', state.onEnd);
|
||||
socket.on('close', state.onClose);
|
||||
socket.on('drain', state.onDrain);
|
||||
parser.onIncoming = parserOnIncoming.bind(undefined, this, socket, state);
|
||||
parser.onIncoming = parserOnIncoming.bind(undefined, server, socket, state);
|
||||
|
||||
// We are consuming socket, so it won't get any actual data
|
||||
socket.on('resume', onSocketResume);
|
||||
@@ -361,7 +371,7 @@ function connectionListener(socket) {
|
||||
}
|
||||
}
|
||||
parser[kOnExecute] =
|
||||
onParserExecute.bind(undefined, this, socket, parser, state);
|
||||
onParserExecute.bind(undefined, server, socket, parser, state);
|
||||
|
||||
socket._paused = false;
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ const async_wrap = process.binding('async_wrap');
|
||||
* It has a fixed size, so if that is exceeded, calls to the native
|
||||
* side are used instead in pushAsyncIds() and popAsyncIds().
|
||||
*/
|
||||
const { async_hook_fields, async_id_fields } = async_wrap;
|
||||
const { async_id_symbol, async_hook_fields, async_id_fields } = async_wrap;
|
||||
// Store the pair executionAsyncId and triggerAsyncId in a std::stack on
|
||||
// Environment::AsyncHooks::ids_stack_ tracks the resource responsible for the
|
||||
// current execution stack. This is unwound as each resource exits. In the case
|
||||
@@ -251,6 +251,15 @@ function newUid() {
|
||||
return ++async_id_fields[kAsyncIdCounter];
|
||||
}
|
||||
|
||||
function getOrSetAsyncId(object) {
|
||||
if (object.hasOwnProperty(async_id_symbol)) {
|
||||
return object[async_id_symbol];
|
||||
}
|
||||
|
||||
return object[async_id_symbol] = newUid();
|
||||
}
|
||||
|
||||
|
||||
// Return the triggerAsyncId meant for the constructor calling it. It's up to
|
||||
// the user to safeguard this call and make sure it's zero'd out when the
|
||||
// constructor is complete.
|
||||
@@ -381,6 +390,7 @@ module.exports = {
|
||||
disableHooks,
|
||||
// Sensitive Embedder API
|
||||
newUid,
|
||||
getOrSetAsyncId,
|
||||
getDefaultTriggerAsyncId,
|
||||
defaultTriggerAsyncIdScope,
|
||||
emitInit: emitInitScript,
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
'use strict';
|
||||
|
||||
const common = require('../common');
|
||||
const initHooks = require('./init-hooks');
|
||||
const verifyGraph = require('./verify-graph');
|
||||
const http = require('http');
|
||||
|
||||
const hooks = initHooks();
|
||||
hooks.enable();
|
||||
|
||||
const server = http.createServer(common.mustCall(function(req, res) {
|
||||
res.end();
|
||||
this.close(common.mustCall());
|
||||
}));
|
||||
server.listen(0, common.mustCall(function() {
|
||||
http.get(`http://127.0.0.1:${server.address().port}`, common.mustCall());
|
||||
}));
|
||||
|
||||
process.on('exit', function() {
|
||||
hooks.disable();
|
||||
|
||||
verifyGraph(
|
||||
hooks,
|
||||
[ { type: 'TCPSERVERWRAP',
|
||||
id: 'tcpserver:1',
|
||||
triggerAsyncId: null },
|
||||
{ type: 'TCPWRAP', id: 'tcp:1', triggerAsyncId: 'tcpserver:1' },
|
||||
{ type: 'TCPCONNECTWRAP',
|
||||
id: 'tcpconnect:1',
|
||||
triggerAsyncId: 'tcp:1' },
|
||||
{ type: 'HTTPPARSER',
|
||||
id: 'httpparser:1',
|
||||
triggerAsyncId: 'tcpserver:1' },
|
||||
{ type: 'HTTPPARSER',
|
||||
id: 'httpparser:2',
|
||||
triggerAsyncId: 'tcpserver:1' },
|
||||
{ type: 'TCPWRAP', id: 'tcp:2', triggerAsyncId: 'tcpserver:1' },
|
||||
{ type: 'Timeout', id: 'timeout:1', triggerAsyncId: 'tcp:2' },
|
||||
{ type: 'TIMERWRAP', id: 'timer:1', triggerAsyncId: 'tcp:2' },
|
||||
{ type: 'HTTPPARSER',
|
||||
id: 'httpparser:3',
|
||||
triggerAsyncId: 'tcp:2' },
|
||||
{ type: 'HTTPPARSER',
|
||||
id: 'httpparser:4',
|
||||
triggerAsyncId: 'tcp:2' },
|
||||
{ type: 'Timeout',
|
||||
id: 'timeout:2',
|
||||
triggerAsyncId: 'httpparser:4' },
|
||||
{ type: 'TIMERWRAP',
|
||||
id: 'timer:2',
|
||||
triggerAsyncId: 'httpparser:4' },
|
||||
{ type: 'SHUTDOWNWRAP',
|
||||
id: 'shutdown:1',
|
||||
triggerAsyncId: 'tcp:2' } ]
|
||||
);
|
||||
});
|
||||
Reference in new issue
Block a user