Skip to content

Commit cb955e0

Browse files
committed
net: create diagnostics channels lazily
PR-URL: #38905 Refs: #35711 Reviewed-By: Chengzhong Wu <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 32ffa5d commit cb955e0

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

lib/net.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,20 @@ const noop = () => {};
131131

132132
const kPerfHooksNetConnectContext = Symbol('kPerfHooksNetConnectContext');
133133

134-
const dc = require('diagnostics_channel');
135-
const netClientSocketChannel = dc.channel('net.client.socket');
136-
const netServerSocketChannel = dc.channel('net.server.socket');
134+
let netClientSocketChannel;
135+
let netServerSocketChannel;
136+
function lazyChannels() {
137+
// TODO(joyeecheung): support diagnostics channels in the snapshot.
138+
// For now it is fine to create them lazily when there isn't a snapshot to
139+
// build. If users need the channels they would have to create them first
140+
// before invoking any built-ins that would publish to these channels
141+
// anyway.
142+
if (netClientSocketChannel === undefined) {
143+
const dc = require('diagnostics_channel');
144+
netClientSocketChannel = dc.channel('net.client.socket');
145+
netServerSocketChannel = dc.channel('net.server.socket');
146+
}
147+
}
137148

138149
const {
139150
hasObserver,
@@ -206,6 +217,7 @@ function connect(...args) {
206217
const options = normalized[0];
207218
debug('createConnection', normalized);
208219
const socket = new Socket(options);
220+
lazyChannels();
209221
if (netClientSocketChannel.hasSubscribers) {
210222
netClientSocketChannel.publish({
211223
socket,
@@ -1739,6 +1751,7 @@ function onconnection(err, clientHandle) {
17391751
socket.server = self;
17401752
socket._server = self;
17411753
self.emit('connection', socket);
1754+
lazyChannels();
17421755
if (netServerSocketChannel.hasSubscribers) {
17431756
netServerSocketChannel.publish({
17441757
socket,

test/parallel/test-bootstrap-modules.js

-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ const expectedModules = new Set([
170170
'NativeModule v8',
171171
'NativeModule internal/v8/startup_snapshot',
172172
'NativeModule vm',
173-
'NativeModule diagnostics_channel',
174173
]);
175174

176175
if (!common.isMainThread) {

0 commit comments

Comments
 (0)