Permalink
Browse files

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>
  • Loading branch information...
bnoordhuis committed Apr 11, 2017
1 parent bb06add commit 5d06e5c30d7350ffe697056bc9a712f8152cecc1
Showing with 11 additions and 3 deletions.
  1. +11 −3 lib/net.js
View
@@ -40,8 +40,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;
@@ -970,7 +971,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;
@@ -1309,6 +1310,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;
@@ -1437,7 +1445,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);

0 comments on commit 5d06e5c

Please sign in to comment.