Browse Source

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.
v0.9.8-release
Fedor Indutny 12 years ago
parent
commit
00abc243dd
  1. 68
      lib/child_process.js
  2. 112
      test/simple/test-child-process-fork-track.js

68
lib/child_process.js

@ -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,10 +116,6 @@ 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
@ -128,7 +124,6 @@ var handleConversion = {
// Act like socket is detached
socket.server._connections--;
}
}
// remove handle from socket object, it will be closed when the socket
// will be sent
@ -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');
});
};

112
test/simple/test-child-process-fork-track.js

@ -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…
Cancel
Save