Skip to content

Commit 1b3fcf7

Browse files
joyeecheungruyadorno
authored andcommitted
net: create diagnostics channels lazily
PR-URL: #38905 Refs: #35711 Reviewed-By: Chengzhong Wu <legendecas@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 514e516 commit 1b3fcf7

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
@@ -135,9 +135,20 @@ const noop = () => {};
135135

136136
const kPerfHooksNetConnectContext = Symbol('kPerfHooksNetConnectContext');
137137

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

142153
const {
143154
hasObserver,
@@ -210,6 +221,7 @@ function connect(...args) {
210221
const options = normalized[0];
211222
debug('createConnection', normalized);
212223
const socket = new Socket(options);
224+
lazyChannels();
213225
if (netClientSocketChannel.hasSubscribers) {
214226
netClientSocketChannel.publish({
215227
socket,
@@ -1756,6 +1768,7 @@ function onconnection(err, clientHandle) {
17561768

17571769
DTRACE_NET_SERVER_CONNECTION(socket);
17581770
self.emit('connection', socket);
1771+
lazyChannels();
17591772
if (netServerSocketChannel.hasSubscribers) {
17601773
netServerSocketChannel.publish({
17611774
socket,

test/parallel/test-bootstrap-modules.js

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

177176
if (!common.isMainThread) {

0 commit comments

Comments
 (0)