diff options
Diffstat (limited to 'lib/net.js')
-rw-r--r-- | lib/net.js | 57 |
1 files changed, 45 insertions, 12 deletions
diff --git a/lib/net.js b/lib/net.js index 41e28195a..4c7916190 100644 --- a/lib/net.js +++ b/lib/net.js @@ -874,8 +874,6 @@ function Server(/* [ options, ] listener */) { this._connections = 0; - // when server is using slaves .connections is not reliable - // so null will be return if thats the case Object.defineProperty(this, 'connections', { get: function() { if (self._usingSlaves) { @@ -890,6 +888,8 @@ function Server(/* [ options, ] listener */) { }); this._handle = null; + this._usingSlaves = false; + this._slaves = []; this.allowHalfOpen = options.allowHalfOpen || false; } @@ -1122,7 +1122,37 @@ function onconnection(clientHandle) { } +Server.prototype.getConnections = function(cb) { + if (!this._usingSlaves) return cb(null, this.connections); + + // Poll slaves + var left = this._slaves.length, + total = this._connections; + + function oncount(err, count) { + if (err) { + left = -1; + return cb(err); + } + + total += count; + if (--left === 0) return cb(null, total); + } + + this._slaves.forEach(function(slave) { + slave.getConnections(oncount); + }); +}; + + Server.prototype.close = function(cb) { + function onSlaveClose() { + if (--left !== 0) return; + + self._connections = 0; + self._emitCloseIfDrained(); + } + if (!this._handle) { // Throw error. Follows net_legacy behaviour. throw new Error('Not running'); @@ -1133,14 +1163,21 @@ Server.prototype.close = function(cb) { } this._handle.close(); this._handle = null; - this._emitCloseIfDrained(); - // fetch new socket lists if (this._usingSlaves) { - this._slaves.forEach(function(socketList) { - if (socketList.list.length === 0) return; - socketList.update(); + var self = this, + left = this._slaves.length; + + // Increment connections to be sure that, even if all sockets will be closed + // during polling of slaves, `close` event will be emitted only once. + this._connections++; + + // Poll slaves + this._slaves.forEach(function(slave) { + slave.close(onSlaveClose); }); + } else { + this._emitCloseIfDrained(); } return this; @@ -1167,12 +1204,8 @@ Server.prototype.listenFD = util.deprecate(function(fd, type) { return this.listen({ fd: fd }); }, 'listenFD is deprecated. Use listen({fd: <number>}).'); -// when sending a socket using fork IPC this function is executed Server.prototype._setupSlave = function(socketList) { - if (!this._usingSlaves) { - this._usingSlaves = true; - this._slaves = []; - } + this._usingSlaves = true; this._slaves.push(socketList); }; |