Skip to content

Commit 5d06e5c

Browse files
committed
net: require 'dns' only once
Avoid unnecessary calls to require('dns') by caching the result of the first one. PR-URL: #12342 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Evan Lucas <evanlucas@me.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent bb06add commit 5d06e5c

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

lib/net.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,9 @@ const PipeConnectWrap = process.binding('pipe_wrap').PipeConnectWrap;
4040
const ShutdownWrap = process.binding('stream_wrap').ShutdownWrap;
4141
const WriteWrap = process.binding('stream_wrap').WriteWrap;
4242

43-
4443
var cluster;
44+
var dns;
45+
4546
const errnoException = util._errnoException;
4647
const exceptionWithHostPort = util._exceptionWithHostPort;
4748
const isLegalPort = internalNet.isLegalPort;
@@ -970,7 +971,7 @@ function realConnect(options, cb) {
970971

971972

972973
function lookupAndConnect(self, options) {
973-
const dns = require('dns');
974+
const dns = lazyDns();
974975
var host = options.host || 'localhost';
975976
var port = options.port;
976977
var localAddress = options.localAddress;
@@ -1309,6 +1310,13 @@ function emitListeningNT(self) {
13091310
}
13101311

13111312

1313+
function lazyDns() {
1314+
if (dns === undefined)
1315+
dns = require('dns');
1316+
return dns;
1317+
}
1318+
1319+
13121320
function listenInCluster(server, address, port, addressType,
13131321
backlog, fd, exclusive) {
13141322
exclusive = !!exclusive;
@@ -1437,7 +1445,7 @@ Server.prototype.listen = function() {
14371445
};
14381446

14391447
function lookupAndListen(self, port, address, backlog, exclusive) {
1440-
const dns = require('dns');
1448+
const dns = lazyDns();
14411449
dns.lookup(address, function doListen(err, ip, addressType) {
14421450
if (err) {
14431451
self.emit('error', err);

0 commit comments

Comments
 (0)