child_process: remove .track option
Since net.Server's `connection` property is deprecated now, we don't need API to track socket's state to keep `connection`s value up-to-date.
This commit is contained in:
parent
539bf1d7b7
commit
00abc243dd
@ -107,7 +107,7 @@ var handleConversion = {
|
||||
},
|
||||
|
||||
'net.Socket': {
|
||||
send: function(message, socket, options) {
|
||||
send: function(message, socket) {
|
||||
// if the socket was created by net.Server
|
||||
if (socket.server) {
|
||||
// the slave should keep track of the socket
|
||||
@ -116,18 +116,13 @@ var handleConversion = {
|
||||
var firstTime = !this._channel.sockets.send[message.key];
|
||||
var socketList = getSocketList('send', this, message.key);
|
||||
|
||||
if (options && options.track) {
|
||||
// Keep track of socket's status
|
||||
message.id = socketList.add(socket);
|
||||
} else {
|
||||
// the server should no longer expose a .connection property
|
||||
// and when asked to close it should query the socket status from
|
||||
// the slaves
|
||||
if (firstTime) socket.server._setupSlave(socketList);
|
||||
// the server should no longer expose a .connection property
|
||||
// and when asked to close it should query the socket status from
|
||||
// the slaves
|
||||
if (firstTime) socket.server._setupSlave(socketList);
|
||||
|
||||
// Act like socket is detached
|
||||
socket.server._connections--;
|
||||
}
|
||||
// Act like socket is detached
|
||||
socket.server._connections--;
|
||||
}
|
||||
|
||||
// remove handle from socket object, it will be closed when the socket
|
||||
@ -154,7 +149,6 @@ var handleConversion = {
|
||||
// add socket to connections list
|
||||
var socketList = getSocketList('got', this, message.key);
|
||||
socketList.add({
|
||||
id: message.id,
|
||||
socket: socket
|
||||
});
|
||||
}
|
||||
@ -172,60 +166,9 @@ function SocketListSend(slave, key) {
|
||||
|
||||
this.key = key;
|
||||
this.slave = slave;
|
||||
|
||||
// These two arrays are used to store the list of sockets and the freelist of
|
||||
// indexes in this list. After insertion, item will have persistent index
|
||||
// until it's removed. This way we can use this index as an identifier for
|
||||
// sockets.
|
||||
this.list = [];
|
||||
this.freelist = [];
|
||||
|
||||
slave.once('disconnect', function() {
|
||||
self.flush();
|
||||
});
|
||||
|
||||
this.slave.on('internalMessage', function(msg) {
|
||||
if (msg.cmd !== 'NODE_SOCKET_CLOSED' || msg.key !== self.key) return;
|
||||
self.remove(msg.id);
|
||||
});
|
||||
}
|
||||
util.inherits(SocketListSend, EventEmitter);
|
||||
|
||||
SocketListSend.prototype.add = function(socket) {
|
||||
var index;
|
||||
|
||||
// Pick one of free indexes, or insert in the end of the list
|
||||
if (this.freelist.length > 0) {
|
||||
index = this.freelist.pop();
|
||||
this.list[index] = socket;
|
||||
} else {
|
||||
index = this.list.push(socket) - 1;
|
||||
}
|
||||
|
||||
return index;
|
||||
};
|
||||
|
||||
SocketListSend.prototype.remove = function(index) {
|
||||
var socket = this.list[index];
|
||||
if (!socket) return;
|
||||
|
||||
// Create a hole in the list and move index to the freelist
|
||||
this.list[index] = null;
|
||||
this.freelist.push(index);
|
||||
|
||||
socket.destroy();
|
||||
};
|
||||
|
||||
SocketListSend.prototype.flush = function() {
|
||||
var list = this.list;
|
||||
this.list = [];
|
||||
this.freelist = [];
|
||||
|
||||
list.forEach(function(socket) {
|
||||
if (socket) socket.destroy();
|
||||
});
|
||||
};
|
||||
|
||||
SocketListSend.prototype._request = function(msg, cmd, callback) {
|
||||
var self = this;
|
||||
|
||||
@ -315,15 +258,6 @@ SocketListReceive.prototype.add = function(obj) {
|
||||
obj.socket.once('close', function() {
|
||||
self.connections--;
|
||||
|
||||
if (obj.id !== undefined && self.slave.connected) {
|
||||
// Master wants to keep eye on socket status
|
||||
self.slave.send({
|
||||
cmd: 'NODE_SOCKET_CLOSED',
|
||||
key: self.key,
|
||||
id: obj.id
|
||||
});
|
||||
}
|
||||
|
||||
if (self.connections === 0) self.emit('empty');
|
||||
});
|
||||
};
|
||||
|
@ -1,112 +0,0 @@
|
||||
// Copyright Joyent, Inc. and other Node contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||
// persons to whom the Software is furnished to do so, subject to the
|
||||
// following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
var assert = require('assert');
|
||||
var common = require('../common');
|
||||
var fork = require('child_process').fork;
|
||||
var net = require('net');
|
||||
var count = 12;
|
||||
|
||||
if (process.argv[2] === 'child') {
|
||||
var sockets = [];
|
||||
var id = process.argv[3];
|
||||
|
||||
process.on('message', function(m, socket) {
|
||||
if (socket) {
|
||||
sockets.push(socket);
|
||||
}
|
||||
|
||||
if (m.cmd === 'close') {
|
||||
sockets[m.id].once('close', function() {
|
||||
process.send({ id: m.id, status: 'closed' });
|
||||
});
|
||||
sockets[m.id].destroy();
|
||||
}
|
||||
});
|
||||
} else {
|
||||
var child = fork(process.argv[1], ['child']);
|
||||
|
||||
var server = net.createServer();
|
||||
var sockets = [];
|
||||
var closed = 0;
|
||||
|
||||
server.on('connection', function(socket) {
|
||||
child.send({ cmd: 'new' }, socket, { track: true });
|
||||
sockets.push(socket);
|
||||
|
||||
socket.once('close', function() {
|
||||
console.error('[m] socket closed');
|
||||
closed++;
|
||||
server.getConnections(function(err, num) {
|
||||
assert(!err);
|
||||
assert.equal(closed + num, count);
|
||||
if (num === 0) server.close();
|
||||
});
|
||||
});
|
||||
|
||||
if (sockets.length === count) {
|
||||
closeSockets(0);
|
||||
}
|
||||
});
|
||||
|
||||
var disconnected = 0;
|
||||
server.on('listening', function() {
|
||||
|
||||
var j = count, client;
|
||||
while (j--) {
|
||||
client = net.connect(common.PORT, '127.0.0.1');
|
||||
client.on('close', function() {
|
||||
console.error('[m] CLIENT: close event');
|
||||
disconnected += 1;
|
||||
});
|
||||
// XXX This resume() should be unnecessary.
|
||||
// a stream high water mark should be enough to keep
|
||||
// consuming the input.
|
||||
client.resume();
|
||||
}
|
||||
});
|
||||
|
||||
function closeSockets(i) {
|
||||
if (i === count) return;
|
||||
|
||||
child.send({ id: i, cmd: 'close' });
|
||||
child.once('message', function(m) {
|
||||
assert(m.status === 'closed');
|
||||
closeSockets(i + 1);
|
||||
});
|
||||
};
|
||||
|
||||
var closeEmitted = false;
|
||||
server.on('close', function() {
|
||||
console.error('[m] server close');
|
||||
closeEmitted = true;
|
||||
|
||||
child.kill();
|
||||
});
|
||||
|
||||
server.listen(common.PORT, '127.0.0.1');
|
||||
|
||||
process.on('exit', function() {
|
||||
assert.equal(disconnected, count);
|
||||
assert.equal(closed, count);
|
||||
assert.ok(closeEmitted);
|
||||
});
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user