Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net: micro cleanups #12342

Merged
merged 6 commits into from
Apr 18, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 28 additions & 25 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -94,7 +95,7 @@ function connect() {
socket.setTimeout(options.timeout);
}

return Socket.prototype.connect.call(socket, options, cb);
return realConnect.call(socket, options, cb);
}


Expand Down Expand Up @@ -498,7 +499,7 @@ Socket.prototype.destroySoon = function() {
Socket.prototype._destroy = function(exception, cb) {
debug('destroy');

function fireErrorCallbacks(self) {
function fireErrorCallbacks(self, exception, cb) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this is done for performance, but does shadowing negate that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean shadowing of variable/argument names? No, should not be a factor here.

if (cb) cb(exception);
if (exception && !self._writableState.errorEmitted) {
process.nextTick(emitErrorNT, self, exception);
Expand All @@ -508,7 +509,7 @@ Socket.prototype._destroy = function(exception, cb) {

if (this.destroyed) {
debug('already destroyed, fire error callbacks');
fireErrorCallbacks(this);
fireErrorCallbacks(this, exception, cb);
return;
}

Expand Down Expand Up @@ -540,7 +541,7 @@ Socket.prototype._destroy = function(exception, cb) {
// to make it re-entrance safe in case Socket.prototype.destroy()
// is called within callbacks
this.destroyed = true;
fireErrorCallbacks(this);
fireErrorCallbacks(this, exception, cb);

if (this._server) {
COUNTER_NET_SERVER_CONNECTION_CLOSE(this);
Expand Down Expand Up @@ -858,26 +859,20 @@ function internalConnect(
var err;

if (localAddress || localPort) {
var bind;
debug('binding to localAddress: %s and localPort: %d (addressType: %d)',
localAddress, localPort, addressType);

if (addressType === 4) {
localAddress = localAddress || '0.0.0.0';
bind = self._handle.bind;
err = self._handle.bind(localAddress, localPort);
} else if (addressType === 6) {
localAddress = localAddress || '::';
bind = self._handle.bind6;
err = self._handle.bind6(localAddress, localPort);
} else {
self._destroy(new TypeError('Invalid addressType: ' + addressType));
return;
}

debug('binding to localAddress: %s and localPort: %d',
localAddress,
localPort);

bind = bind.bind(self._handle);
err = bind(localAddress, localPort);

if (err) {
const ex = exceptionWithHostPort(err, 'bind', localAddress, localPort);
self._destroy(ex);
Expand Down Expand Up @@ -927,7 +922,11 @@ Socket.prototype.connect = function() {
const normalized = normalizeArgs(args);
const options = normalized[0];
const cb = normalized[1];
return realConnect.call(this, options, cb);
};


function realConnect(options, cb) {
if (this.write !== Socket.prototype.write)
this.write = Socket.prototype.write;

Expand Down Expand Up @@ -968,11 +967,11 @@ Socket.prototype.connect = function() {
lookupAndConnect(this, options);
}
return this;
};
}


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 All @@ -997,10 +996,7 @@ function lookupAndConnect(self, options) {
// If host is an IP, skip performing a lookup
var addressType = cares.isIP(host);
if (addressType) {
process.nextTick(function() {
if (self.connecting)
internalConnect(self, host, port, addressType, localAddress, localPort);
});
internalConnect(self, host, port, addressType, localAddress, localPort);
return;
}

Expand All @@ -1016,7 +1012,7 @@ function lookupAndConnect(self, options) {
dnsopts.hints = dns.ADDRCONFIG;
}

debug('connect: find host ' + host);
debug('connect: find host', host);
debug('connect: dns options', dnsopts);
self._host = host;
var lookup = options.lookup || dns.lookup;
Expand Down Expand Up @@ -1192,7 +1188,7 @@ function createServerHandle(address, port, addressType, fd) {
handle = createHandle(fd);
} catch (e) {
// Not a fd we can listen on. This will trigger an error.
debug('listen invalid fd=' + fd + ': ' + e.message);
debug('listen invalid fd=%d:', fd, e.message);
return uv.UV_EINVAL;
}
handle.open(fd);
Expand All @@ -1213,7 +1209,7 @@ function createServerHandle(address, port, addressType, fd) {
}

if (address || port || isTCP) {
debug('bind to ' + (address || 'anycast'));
debug('bind to', address || 'any');
if (!address) {
// Try binding to ipv6 first
err = handle.bind6('::', port);
Expand Down Expand Up @@ -1314,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;
Expand Down Expand Up @@ -1442,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);
Expand Down