From 1b3fcf765f867e246f1b04b1b834ce2d5536d8fb Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Tue, 2 Aug 2022 19:22:51 +0800 Subject: [PATCH] net: create diagnostics channels lazily PR-URL: https://github.com/nodejs/node/pull/38905 Refs: https://github.com/nodejs/node/issues/35711 Reviewed-By: Chengzhong Wu Reviewed-By: Matteo Collina --- lib/net.js | 19 ++++++++++++++++--- test/parallel/test-bootstrap-modules.js | 1 - 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/net.js b/lib/net.js index 8c8d36ec8a8c8e..ee28ddcbde8817 100644 --- a/lib/net.js +++ b/lib/net.js @@ -135,9 +135,20 @@ const noop = () => {}; const kPerfHooksNetConnectContext = Symbol('kPerfHooksNetConnectContext'); -const dc = require('diagnostics_channel'); -const netClientSocketChannel = dc.channel('net.client.socket'); -const netServerSocketChannel = dc.channel('net.server.socket'); +let netClientSocketChannel; +let netServerSocketChannel; +function lazyChannels() { + // TODO(joyeecheung): support diagnostics channels in the snapshot. + // For now it is fine to create them lazily when there isn't a snapshot to + // build. If users need the channels they would have to create them first + // before invoking any built-ins that would publish to these channels + // anyway. + if (netClientSocketChannel === undefined) { + const dc = require('diagnostics_channel'); + netClientSocketChannel = dc.channel('net.client.socket'); + netServerSocketChannel = dc.channel('net.server.socket'); + } +} const { hasObserver, @@ -210,6 +221,7 @@ function connect(...args) { const options = normalized[0]; debug('createConnection', normalized); const socket = new Socket(options); + lazyChannels(); if (netClientSocketChannel.hasSubscribers) { netClientSocketChannel.publish({ socket, @@ -1756,6 +1768,7 @@ function onconnection(err, clientHandle) { DTRACE_NET_SERVER_CONNECTION(socket); self.emit('connection', socket); + lazyChannels(); if (netServerSocketChannel.hasSubscribers) { netServerSocketChannel.publish({ socket, diff --git a/test/parallel/test-bootstrap-modules.js b/test/parallel/test-bootstrap-modules.js index 3df6d9679aeb8d..63ef4336123640 100644 --- a/test/parallel/test-bootstrap-modules.js +++ b/test/parallel/test-bootstrap-modules.js @@ -171,7 +171,6 @@ const expectedModules = new Set([ 'NativeModule v8', 'NativeModule internal/v8/startup_snapshot', 'NativeModule vm', - 'NativeModule diagnostics_channel', ]); if (!common.isMainThread) {