Skip to content

Commit

Permalink
net: require 'dns' only once
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
bnoordhuis authored and evanlucas committed Apr 25, 2017
1 parent ae51ddd commit 2957449
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ const PipeConnectWrap = process.binding('pipe_wrap').PipeConnectWrap;
const ShutdownWrap = process.binding('stream_wrap').ShutdownWrap;
const WriteWrap = process.binding('stream_wrap').WriteWrap;


var cluster;
var dns;

const errnoException = util._errnoException;
const exceptionWithHostPort = util._exceptionWithHostPort;
const isLegalPort = internalNet.isLegalPort;
Expand Down Expand Up @@ -948,7 +949,7 @@ function realConnect(options, cb) {


function lookupAndConnect(self, options) {
const dns = require('dns');
const dns = lazyDns();
var host = options.host || 'localhost';
var port = options.port;
var localAddress = options.localAddress;
Expand Down Expand Up @@ -1286,6 +1287,13 @@ function emitListeningNT(self) {
}


function lazyDns() {
if (dns === undefined)
dns = require('dns');
return dns;
}


function listenInCluster(server, address, port, addressType,
backlog, fd, exclusive) {
exclusive = !!exclusive;
Expand Down Expand Up @@ -1414,7 +1422,7 @@ Server.prototype.listen = function() {
};

function lookupAndListen(self, port, address, backlog, exclusive) {
const dns = require('dns');
const dns = lazyDns();
dns.lookup(address, function doListen(err, ip, addressType) {
if (err) {
self.emit('error', err);
Expand Down

0 comments on commit 2957449

Please sign in to comment.