Skip to content

Commit 0ea4570

Browse files
committed
net: rename internal functions for readability
* Rename listen to listenInCluster * Rename _listen2 to _setupListenHandle * Remove _listen since it's a one-liner only used in one place * Correct comments in server.listen PR-URL: #11796 Reviewed-By: James M Snell <[email protected]>
1 parent 0e710aa commit 0ea4570

File tree

1 file changed

+43
-30
lines changed

1 file changed

+43
-30
lines changed

lib/net.js

+43-30
Original file line numberDiff line numberDiff line change
@@ -342,10 +342,11 @@ Socket.prototype.read = function(n) {
342342
};
343343

344344

345+
// FIXME(joyeecheung): this method is neither documented nor tested
345346
Socket.prototype.listen = function() {
346347
debug('socket.listen');
347348
this.on('connection', arguments[0]);
348-
listen(this, null, null, null);
349+
listenInCluster(this, null, null, null);
349350
};
350351

351352

@@ -1178,13 +1179,7 @@ util.inherits(Server, EventEmitter);
11781179

11791180
function toNumber(x) { return (x = Number(x)) >= 0 ? x : false; }
11801181

1181-
function _listen(handle, backlog) {
1182-
// Use a backlog of 512 entries. We pass 511 to the listen() call because
1183-
// the kernel does: backlogsize = roundup_pow_of_two(backlogsize + 1);
1184-
// which will thus give us a backlog of 512 entries.
1185-
return handle.listen(backlog || 511);
1186-
}
1187-
1182+
// Returns handle if it can be created, or error code if it can't
11881183
function createServerHandle(address, port, addressType, fd) {
11891184
var err = 0;
11901185
// assign handle in listen, and clean up if bind or listen fails
@@ -1241,19 +1236,19 @@ function createServerHandle(address, port, addressType, fd) {
12411236
return handle;
12421237
}
12431238

1244-
1245-
Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
1246-
debug('listen2', address, port, addressType, backlog, fd);
1239+
function setupListenHandle(address, port, addressType, backlog, fd) {
1240+
debug('setupListenHandle', address, port, addressType, backlog, fd);
12471241

12481242
// If there is not yet a handle, we need to create one and bind.
12491243
// In the case of a server sent via IPC, we don't need to do this.
12501244
if (this._handle) {
1251-
debug('_listen2: have a handle already');
1245+
debug('setupListenHandle: have a handle already');
12521246
} else {
1253-
debug('_listen2: create a handle');
1247+
debug('setupListenHandle: create a handle');
12541248

12551249
var rval = null;
12561250

1251+
// Try to bind to the unspecified IPv6 address, see if IPv6 is available
12571252
if (!address && typeof fd !== 'number') {
12581253
rval = createServerHandle('::', port, 6, fd);
12591254

@@ -1281,7 +1276,10 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
12811276
this._handle.onconnection = onconnection;
12821277
this._handle.owner = this;
12831278

1284-
var err = _listen(this._handle, backlog);
1279+
// Use a backlog of 512 entries. We pass 511 to the listen() call because
1280+
// the kernel does: backlogsize = roundup_pow_of_two(backlogsize + 1);
1281+
// which will thus give us a backlog of 512 entries.
1282+
var err = this._handle.listen(backlog || 511);
12851283

12861284
if (err) {
12871285
var ex = exceptionWithHostPort(err, 'listen', address, port);
@@ -1299,8 +1297,9 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
12991297
this.unref();
13001298

13011299
process.nextTick(emitListeningNT, this);
1302-
};
1300+
}
13031301

1302+
Server.prototype._listen2 = setupListenHandle; // legacy alias
13041303

13051304
function emitErrorNT(self, err) {
13061305
self.emit('error', err);
@@ -1314,25 +1313,32 @@ function emitListeningNT(self) {
13141313
}
13151314

13161315

1317-
function listen(self, address, port, addressType, backlog, fd, exclusive) {
1316+
function listenInCluster(server, address, port, addressType,
1317+
backlog, fd, exclusive) {
13181318
exclusive = !!exclusive;
13191319

13201320
if (!cluster) cluster = require('cluster');
13211321

13221322
if (cluster.isMaster || exclusive) {
1323-
self._listen2(address, port, addressType, backlog, fd);
1323+
// Will create a new handle
1324+
// _listen2 sets up the listened handle, it is still named like this
1325+
// to avoid breaking code that wraps this method
1326+
server._listen2(address, port, addressType, backlog, fd);
13241327
return;
13251328
}
13261329

1327-
cluster._getServer(self, {
1330+
const serverQuery = {
13281331
address: address,
13291332
port: port,
13301333
addressType: addressType,
13311334
fd: fd,
13321335
flags: 0
1333-
}, cb);
1336+
};
1337+
1338+
// Get the master's server handle, and listen on it
1339+
cluster._getServer(server, serverQuery, listenOnMasterHandle);
13341340

1335-
function cb(err, handle) {
1341+
function listenOnMasterHandle(err, handle) {
13361342
// EADDRINUSE may not be reported until we call listen(). To complicate
13371343
// matters, a failed bind() followed by listen() will implicitly bind to
13381344
// a random port. Ergo, check that the socket is bound to the expected
@@ -1350,11 +1356,14 @@ function listen(self, address, port, addressType, backlog, fd, exclusive) {
13501356

13511357
if (err) {
13521358
var ex = exceptionWithHostPort(err, 'bind', address, port);
1353-
return self.emit('error', ex);
1359+
return server.emit('error', ex);
13541360
}
13551361

1356-
self._handle = handle;
1357-
self._listen2(address, port, addressType, backlog, fd);
1362+
// Reuse master's server handle
1363+
server._handle = handle;
1364+
// _listen2 sets up the listened handle, it is still named like this
1365+
// to avoid breaking code that wraps this method
1366+
server._listen2(address, port, addressType, backlog, fd);
13581367
}
13591368
}
13601369

@@ -1381,12 +1390,12 @@ Server.prototype.listen = function() {
13811390
// (handle[, backlog][, cb]) where handle is an object with a handle
13821391
if (options instanceof TCP) {
13831392
this._handle = options;
1384-
listen(this, null, -1, -1, backlogFromArgs);
1393+
listenInCluster(this, null, -1, -1, backlogFromArgs);
13851394
return this;
13861395
}
13871396
// (handle[, backlog][, cb]) where handle is an object with a fd
13881397
if (typeof options.fd === 'number' && options.fd >= 0) {
1389-
listen(this, null, null, null, backlogFromArgs, options.fd);
1398+
listenInCluster(this, null, null, null, backlogFromArgs, options.fd);
13901399
return this;
13911400
}
13921401

@@ -1411,8 +1420,9 @@ Server.prototype.listen = function() {
14111420
lookupAndListen(this, options.port | 0, options.host, backlog,
14121421
options.exclusive);
14131422
} else { // Undefined host, listens on unspecified address
1414-
listen(this, null, options.port | 0, 4, // addressType will be ignored
1415-
backlog, undefined, options.exclusive);
1423+
// Default addressType 4 will be used to search for master server
1424+
listenInCluster(this, null, options.port | 0, 4,
1425+
backlog, undefined, options.exclusive);
14161426
}
14171427
return this;
14181428
}
@@ -1422,20 +1432,23 @@ Server.prototype.listen = function() {
14221432
if (options.path && isPipeName(options.path)) {
14231433
const pipeName = this._pipeName = options.path;
14241434
const backlog = options.backlog || backlogFromArgs;
1425-
listen(this, pipeName, -1, -1, backlog, undefined, options.exclusive);
1435+
listenInCluster(this, pipeName, -1, -1,
1436+
backlog, undefined, options.exclusive);
14261437
return this;
14271438
}
14281439

14291440
throw new Error('Invalid listen argument: ' + util.inspect(options));
14301441
};
14311442

14321443
function lookupAndListen(self, port, address, backlog, exclusive) {
1433-
require('dns').lookup(address, function doListening(err, ip, addressType) {
1444+
const dns = require('dns');
1445+
dns.lookup(address, function doListen(err, ip, addressType) {
14341446
if (err) {
14351447
self.emit('error', err);
14361448
} else {
14371449
addressType = ip ? addressType : 4;
1438-
listen(self, ip, port, addressType, backlog, undefined, exclusive);
1450+
listenInCluster(self, ip, port, addressType,
1451+
backlog, undefined, exclusive);
14391452
}
14401453
});
14411454
}

0 commit comments

Comments
 (0)