Skip to content

Commit 9db4f19

Browse files
bnoordhuisevanlucas
authored andcommittedMay 2, 2017
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 <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 94385c6 commit 9db4f19

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed
 

‎lib/net.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ const PipeConnectWrap = process.binding('pipe_wrap').PipeConnectWrap;
1919
const ShutdownWrap = process.binding('stream_wrap').ShutdownWrap;
2020
const WriteWrap = process.binding('stream_wrap').WriteWrap;
2121

22-
2322
var cluster;
23+
var dns;
24+
2425
const errnoException = util._errnoException;
2526
const exceptionWithHostPort = util._exceptionWithHostPort;
2627
const isLegalPort = internalNet.isLegalPort;
@@ -948,7 +949,7 @@ function realConnect(options, cb) {
948949

949950

950951
function lookupAndConnect(self, options) {
951-
const dns = require('dns');
952+
const dns = lazyDns();
952953
var host = options.host || 'localhost';
953954
var port = options.port;
954955
var localAddress = options.localAddress;
@@ -1286,6 +1287,13 @@ function emitListeningNT(self) {
12861287
}
12871288

12881289

1290+
function lazyDns() {
1291+
if (dns === undefined)
1292+
dns = require('dns');
1293+
return dns;
1294+
}
1295+
1296+
12891297
function listenInCluster(server, address, port, addressType,
12901298
backlog, fd, exclusive) {
12911299
exclusive = !!exclusive;
@@ -1414,7 +1422,7 @@ Server.prototype.listen = function() {
14141422
};
14151423

14161424
function lookupAndListen(self, port, address, backlog, exclusive) {
1417-
const dns = require('dns');
1425+
const dns = lazyDns();
14181426
dns.lookup(address, function doListen(err, ip, addressType) {
14191427
if (err) {
14201428
self.emit('error', err);

0 commit comments

Comments
 (0)
Please sign in to comment.