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 <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
joyeecheung authored and danielleadams committed Aug 11, 2022
1 parent befe01c commit 5cb5c65
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,22 @@ const isWindows = process.platform === 'win32';
const noop = () => {};

const kPerfHooksNetConnectContext = Symbol('kPerfHooksNetConnectContext');

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,
startPerf,
Expand Down Expand Up @@ -205,7 +221,7 @@ function connect(...args) {
const options = normalized[0];
debug('createConnection', normalized);
const socket = new Socket(options);

lazyChannels();
if (options.timeout) {
socket.setTimeout(options.timeout);
}
Expand Down Expand Up @@ -1737,6 +1753,7 @@ function onconnection(err, clientHandle) {

DTRACE_NET_SERVER_CONNECTION(socket);
self.emit('connection', socket);
lazyChannels();
}

/**
Expand Down

0 comments on commit 5cb5c65

Please sign in to comment.