Browse Source

net: require 'dns' only once

Avoid unnecessary calls to require('dns') by caching the result of the
first one.

PR-URL: https://github.com/nodejs/node/pull/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>
v7.x
Ben Noordhuis 8 years ago
committed by Evan Lucas
parent
commit
9db4f19283
  1. 14
      lib/net.js

14
lib/net.js

@ -19,8 +19,9 @@ const PipeConnectWrap = process.binding('pipe_wrap').PipeConnectWrap;
const ShutdownWrap = process.binding('stream_wrap').ShutdownWrap; const ShutdownWrap = process.binding('stream_wrap').ShutdownWrap;
const WriteWrap = process.binding('stream_wrap').WriteWrap; const WriteWrap = process.binding('stream_wrap').WriteWrap;
var cluster; var cluster;
var dns;
const errnoException = util._errnoException; const errnoException = util._errnoException;
const exceptionWithHostPort = util._exceptionWithHostPort; const exceptionWithHostPort = util._exceptionWithHostPort;
const isLegalPort = internalNet.isLegalPort; const isLegalPort = internalNet.isLegalPort;
@ -948,7 +949,7 @@ function realConnect(options, cb) {
function lookupAndConnect(self, options) { function lookupAndConnect(self, options) {
const dns = require('dns'); const dns = lazyDns();
var host = options.host || 'localhost'; var host = options.host || 'localhost';
var port = options.port; var port = options.port;
var localAddress = options.localAddress; var localAddress = options.localAddress;
@ -1286,6 +1287,13 @@ function emitListeningNT(self) {
} }
function lazyDns() {
if (dns === undefined)
dns = require('dns');
return dns;
}
function listenInCluster(server, address, port, addressType, function listenInCluster(server, address, port, addressType,
backlog, fd, exclusive) { backlog, fd, exclusive) {
exclusive = !!exclusive; exclusive = !!exclusive;
@ -1414,7 +1422,7 @@ Server.prototype.listen = function() {
}; };
function lookupAndListen(self, port, address, backlog, exclusive) { function lookupAndListen(self, port, address, backlog, exclusive) {
const dns = require('dns'); const dns = lazyDns();
dns.lookup(address, function doListen(err, ip, addressType) { dns.lookup(address, function doListen(err, ip, addressType) {
if (err) { if (err) {
self.emit('error', err); self.emit('error', err);

Loading…
Cancel
Save