Skip to content

Commit

Permalink
net: create diagnostics channels lazily
Browse files Browse the repository at this point in the history
PR-URL: #38905
Refs: #35711
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
joyeecheung authored and ruyadorno committed Aug 23, 2022
1 parent 514e516 commit 1b3fcf7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
19 changes: 16 additions & 3 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1756,6 +1768,7 @@ function onconnection(err, clientHandle) {

DTRACE_NET_SERVER_CONNECTION(socket);
self.emit('connection', socket);
lazyChannels();
if (netServerSocketChannel.hasSubscribers) {
netServerSocketChannel.publish({
socket,
Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-bootstrap-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ const expectedModules = new Set([
'NativeModule v8',
'NativeModule internal/v8/startup_snapshot',
'NativeModule vm',
'NativeModule diagnostics_channel',
]);

if (!common.isMainThread) {
Expand Down

0 comments on commit 1b3fcf7

Please sign in to comment.