Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 305f822

Browse filesBrowse files
joyeecheungItalo A. Casas
authored andcommitted
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 <jasnell@gmail.com>
1 parent 2f88de1 commit 305f822
Copy full SHA for 305f822

File tree

Expand file treeCollapse file tree

1 file changed

+43
-30
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+43
-30
lines changed
Open diff view settings
Collapse file

‎lib/net.js‎

Copy file name to clipboardExpand all lines: lib/net.js
+43-30Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,11 @@ Socket.prototype.read = function(n) {
321321
};
322322

323323

324+
// FIXME(joyeecheung): this method is neither documented nor tested
324325
Socket.prototype.listen = function() {
325326
debug('socket.listen');
326327
this.on('connection', arguments[0]);
327-
listen(this, null, null, null);
328+
listenInCluster(this, null, null, null);
328329
};
329330

330331

@@ -1156,13 +1157,7 @@ util.inherits(Server, EventEmitter);
11561157

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

1159-
function _listen(handle, backlog) {
1160-
// Use a backlog of 512 entries. We pass 511 to the listen() call because
1161-
// the kernel does: backlogsize = roundup_pow_of_two(backlogsize + 1);
1162-
// which will thus give us a backlog of 512 entries.
1163-
return handle.listen(backlog || 511);
1164-
}
1165-
1160+
// Returns handle if it can be created, or error code if it can't
11661161
function createServerHandle(address, port, addressType, fd) {
11671162
var err = 0;
11681163
// assign handle in listen, and clean up if bind or listen fails
@@ -1219,19 +1214,19 @@ function createServerHandle(address, port, addressType, fd) {
12191214
return handle;
12201215
}
12211216

1222-
1223-
Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
1224-
debug('listen2', address, port, addressType, backlog, fd);
1217+
function setupListenHandle(address, port, addressType, backlog, fd) {
1218+
debug('setupListenHandle', address, port, addressType, backlog, fd);
12251219

12261220
// If there is not yet a handle, we need to create one and bind.
12271221
// In the case of a server sent via IPC, we don't need to do this.
12281222
if (this._handle) {
1229-
debug('_listen2: have a handle already');
1223+
debug('setupListenHandle: have a handle already');
12301224
} else {
1231-
debug('_listen2: create a handle');
1225+
debug('setupListenHandle: create a handle');
12321226

12331227
var rval = null;
12341228

1229+
// Try to bind to the unspecified IPv6 address, see if IPv6 is available
12351230
if (!address && typeof fd !== 'number') {
12361231
rval = createServerHandle('::', port, 6, fd);
12371232

@@ -1259,7 +1254,10 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
12591254
this._handle.onconnection = onconnection;
12601255
this._handle.owner = this;
12611256

1262-
var err = _listen(this._handle, backlog);
1257+
// Use a backlog of 512 entries. We pass 511 to the listen() call because
1258+
// the kernel does: backlogsize = roundup_pow_of_two(backlogsize + 1);
1259+
// which will thus give us a backlog of 512 entries.
1260+
var err = this._handle.listen(backlog || 511);
12631261

12641262
if (err) {
12651263
var ex = exceptionWithHostPort(err, 'listen', address, port);
@@ -1277,8 +1275,9 @@ Server.prototype._listen2 = function(address, port, addressType, backlog, fd) {
12771275
this.unref();
12781276

12791277
process.nextTick(emitListeningNT, this);
1280-
};
1278+
}
12811279

1280+
Server.prototype._listen2 = setupListenHandle; // legacy alias
12821281

12831282
function emitErrorNT(self, err) {
12841283
self.emit('error', err);
@@ -1292,25 +1291,32 @@ function emitListeningNT(self) {
12921291
}
12931292

12941293

1295-
function listen(self, address, port, addressType, backlog, fd, exclusive) {
1294+
function listenInCluster(server, address, port, addressType,
1295+
backlog, fd, exclusive) {
12961296
exclusive = !!exclusive;
12971297

12981298
if (!cluster) cluster = require('cluster');
12991299

13001300
if (cluster.isMaster || exclusive) {
1301-
self._listen2(address, port, addressType, backlog, fd);
1301+
// Will create a new handle
1302+
// _listen2 sets up the listened handle, it is still named like this
1303+
// to avoid breaking code that wraps this method
1304+
server._listen2(address, port, addressType, backlog, fd);
13021305
return;
13031306
}
13041307

1305-
cluster._getServer(self, {
1308+
const serverQuery = {
13061309
address: address,
13071310
port: port,
13081311
addressType: addressType,
13091312
fd: fd,
13101313
flags: 0
1311-
}, cb);
1314+
};
1315+
1316+
// Get the master's server handle, and listen on it
1317+
cluster._getServer(server, serverQuery, listenOnMasterHandle);
13121318

1313-
function cb(err, handle) {
1319+
function listenOnMasterHandle(err, handle) {
13141320
// EADDRINUSE may not be reported until we call listen(). To complicate
13151321
// matters, a failed bind() followed by listen() will implicitly bind to
13161322
// a random port. Ergo, check that the socket is bound to the expected
@@ -1328,11 +1334,14 @@ function listen(self, address, port, addressType, backlog, fd, exclusive) {
13281334

13291335
if (err) {
13301336
var ex = exceptionWithHostPort(err, 'bind', address, port);
1331-
return self.emit('error', ex);
1337+
return server.emit('error', ex);
13321338
}
13331339

1334-
self._handle = handle;
1335-
self._listen2(address, port, addressType, backlog, fd);
1340+
// Reuse master's server handle
1341+
server._handle = handle;
1342+
// _listen2 sets up the listened handle, it is still named like this
1343+
// to avoid breaking code that wraps this method
1344+
server._listen2(address, port, addressType, backlog, fd);
13361345
}
13371346
}
13381347

@@ -1359,12 +1368,12 @@ Server.prototype.listen = function() {
13591368
// (handle[, backlog][, cb]) where handle is an object with a handle
13601369
if (options instanceof TCP) {
13611370
this._handle = options;
1362-
listen(this, null, -1, -1, backlogFromArgs);
1371+
listenInCluster(this, null, -1, -1, backlogFromArgs);
13631372
return this;
13641373
}
13651374
// (handle[, backlog][, cb]) where handle is an object with a fd
13661375
if (typeof options.fd === 'number' && options.fd >= 0) {
1367-
listen(this, null, null, null, backlogFromArgs, options.fd);
1376+
listenInCluster(this, null, null, null, backlogFromArgs, options.fd);
13681377
return this;
13691378
}
13701379

@@ -1389,8 +1398,9 @@ Server.prototype.listen = function() {
13891398
lookupAndListen(this, options.port | 0, options.host, backlog,
13901399
options.exclusive);
13911400
} else { // Undefined host, listens on unspecified address
1392-
listen(this, null, options.port | 0, 4, // addressType will be ignored
1393-
backlog, undefined, options.exclusive);
1401+
// Default addressType 4 will be used to search for master server
1402+
listenInCluster(this, null, options.port | 0, 4,
1403+
backlog, undefined, options.exclusive);
13941404
}
13951405
return this;
13961406
}
@@ -1400,20 +1410,23 @@ Server.prototype.listen = function() {
14001410
if (options.path && isPipeName(options.path)) {
14011411
const pipeName = this._pipeName = options.path;
14021412
const backlog = options.backlog || backlogFromArgs;
1403-
listen(this, pipeName, -1, -1, backlog, undefined, options.exclusive);
1413+
listenInCluster(this, pipeName, -1, -1,
1414+
backlog, undefined, options.exclusive);
14041415
return this;
14051416
}
14061417

14071418
throw new Error('Invalid listen argument: ' + options);
14081419
};
14091420

14101421
function lookupAndListen(self, port, address, backlog, exclusive) {
1411-
require('dns').lookup(address, function doListening(err, ip, addressType) {
1422+
const dns = require('dns');
1423+
dns.lookup(address, function doListen(err, ip, addressType) {
14121424
if (err) {
14131425
self.emit('error', err);
14141426
} else {
14151427
addressType = ip ? addressType : 4;
1416-
listen(self, ip, port, addressType, backlog, undefined, exclusive);
1428+
listenInCluster(self, ip, port, addressType,
1429+
backlog, undefined, exclusive);
14171430
}
14181431
});
14191432
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.