@jurisicmarko
February 2017
A single instance of Node.js runs in a single thread. To take advantage of multi-core systems the user will sometimes want to launch a cluster of Node.js processes to handle the load.
The cluster module allows you to easily create child processes that all share server ports.
https://nodejs.org/api/cluster.html
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
// Workers can share any TCP connection
// In this case it is an HTTP server
http.createServer((req, res) => {
console.log('process ' + process.pid +
' processing request');
res.writeHead(200);
res.end('hello world\n' + process.pid);
}).listen(8000);
console.log(`Worker ${process.pid} started`);
}
let fork = require('child_process').fork;
let child = fork('child_process.js');
var fork = require('child_process').fork;
const http = require('http');
http.createServer((req, res) => {
console.log('process ' + process.pid +
' processing request');
res.writeHead(200);
let child = fork('client.js',['foo','bar']);
child.on('message', (msg) => {
res.end('child process:' +
child.pid + ' responded:' + msg );
});
}).listen(8000);
let firstparam = process.argv[2];
let secondparam = process.argv[3];
let result = firstparam + secondparam;
console.log('doing complex stuff at pid',
process.pid, 'got params:', firstparam,
secondparam);
for (i = 0; i < 900000000; i++) {
let z = i*i;
}
if (process.argv[3]) {
process.send('result:' + result)
}
Server
Child
let child = fork(
path.join(__dirname, '../../standalone_export.js'),
[req.user.loginname, JSON.stringify(options)]
);
child.on('message', (msg) => {
status.status.export_running = false;
res.json({message: msg});
});
let exporter = require('./api/exporter');
let username = process.argv[2];
let options = JSON.parse(process.argv[3]);
exporter.exportImages(username, options)
.then(() =>{
process.send('done');
db.disconnect();
})
.catch((err) =>{
process.send('error:' + err);
db.disconnect();
});
Server
Child
const http = require('http');
var ipc = require('node-ipc');
var fork = require('child_process').fork;
ipc.config.id = 'demoserver';
ipc.config.silent = true;
ipc.config.retry = 1500;
fork('client.js');
ipc.serve(() =>{
ipc.server.on('client.started', (messagedata, socket) =>{
console.log('finished comparing data ...');
ipc.server.emit(socket, 'operation.start', {
data: 'this can be any json object'
})
});
ipc.server.on('operation.done', (data, socket) =>{
console.log('finished operation, got result:', data);
console.log('sending shutdown');
ipc.server.emit(socket, 'client.shutdown', {});
ipc.server.stop();
});
});
ipc.server.start();
Server
const ipc = require('node-ipc');
ipc.config.id = 'compare';
ipc.config.retry = 1000;
ipc.config.silent = true;
ipc.connectTo('demoserver', function (){
ipc.of.demoserver.on('connect', () =>{
ipc.of.demoserver.emit('client.started', {});
});
ipc.of.demoserver.on('operation.start', (data) =>{
console.log('got some data:', data);
console.log('starting long operation...');
let result;
for (i = 0; i < 900000000; i++) {
result = i*i;
}
ipc.of.demoserver.emit('operation.done', {result});
});
ipc.of.demoserver.on('client.shutdown', (data) =>{
console.log('got shutdown command, exiting...');
process.exit();
})
});
Client
var ipc = require('node-ipc');
var fork = require('child_process').fork;
var path = require('path');
ipc.config.id = 'pds';
ipc.config.silent = true;
ipc.config.retry = 1500;
var compareData = function(data) {
logger.debug('start comparing data ...');
cache_entry.setState(CacheEntry.COMPARING_DATA);
// fork child node process for comparing, communicate using node-ipc
fork(path.join(__dirname, '../../standalone_compare.js'));
var defer = Q.defer();
ipc.serve(() =>{
ipc.server.on('comparator.started', (messagedata, socket) =>{
ipc.server.emit(socket, 'compare.start', {
import_package: import_package,
importer_instance: importer_instance,
data: data
})
}
);
ipc.server.on('compare.done', (data, socket) =>{
cache_entry.setState(CacheEntry.COMPARED_DATA);
logger.debug('finished comparing data ...');
ipc.server.emit(socket, 'compare.shutdown', {});
ipc.server.stop();
defer.resolve(data.result);
}
)
});
ipc.server.start();
return defer.promise;
};
Server
1
3
5
var ipc = require('node-ipc');
ipc.config.id = 'compare';
ipc.config.retry = 1000;
var compare = require('./api/importer/compare');
ipc.config.silent = true;
ipc.connectTo('pds', function (){
ipc.of.pds.on('connect', () =>{
ipc.of.pds.emit('comparator.started', {});
});
ipc.of.pds.on('compare.start', (data) =>{
let db = require('./api/db').default_connection();
db.connect().then(() =>{
compare.compareData(data.import_package,
data.importer_instance,
data.data).then(result => {
db.disconnect().then(() => {
ipc.of.pds.emit('compare.done', {result});
});
});
});
});
ipc.of.pds.on('compare.shutdown', (data) =>{
process.exit();
})
});
Client
2
4
6
https://github.com/nodejs/node/issues/3145
child_process IPC is very slow, about 100-10000x slower than I expected (scales with msg size) #3145
var ipc = require('node-ipc');
var zlib = require('zlib');
var compare = require('./api/importer/compare');
ipc.config.id = 'compare';
ipc.config.retry = 1000;
ipc.config.rawBuffer = true;
ipc.config.encoding = 'hex';
ipc.config.silent = true;
ipc.connectTo('pds', function (){
ipc.of.pds.on('connect', () =>{
ipc.of.pds.emit(Buffer.from('start', 'utf-8'));
});
ipc.of.pds.on('data', (data) =>{
// got start message
if (data.length == 8) {
console.log('got shutdown message, kill client');
process.exit();
} else {
var db = require('./api/db').default_connection();
db.connect().then(() =>{
zlib.unzip(data, (err, buffer) =>{
if (!err) {
var jsonData = JSON.parse(buffer.toString('utf-8'));
compare.compareData(jsonData.import_package, jsonData.importer_instance, jsonData.data).then(result =>{
db.disconnect().then(() =>{
zlib.deflate(JSON.stringify(result), (err, buffer) =>{
if (!err) {
ipc.of.pds.emit(buffer);
} else {
ipc.of.pds.emit('errror');
console.error('Error', err);
}
});
});
});
} else {
console.error('Error', err);
ipc.of.pds.emit('errror');
}
});
});
}
});
});
Client
var ipc = require('node-ipc');
var zlib = require('zlib');
var compare = require('./api/importer/compare');
var lf = require('ts-node-logging');
var logger = lf.app();
ipc.config.id = 'compare';
ipc.config.retry = 1000;
ipc.config.rawBuffer = true;
ipc.config.encoding = 'hex';
ipc.config.silent = true;
ipc.connectTo('pds', function (){
ipc.of.pds.on('connect', () =>{
ipc.of.pds.emit(new Buffer('start', 'utf-8'));
});
ipc.of.pds.on('data', (data) =>{
// got start message
if (data.length == 8) {
logger.debug('got shutdown message, kill client');
process.exit();
} else {
var db = require('./api/db').default_connection();
db.connect().then(() =>{
zlib.gunzip(data, (err, buffer) =>{
if (!err) {
var jsonData = JSON.parse(buffer.toString('utf-8'));
compare.compareData(jsonData.import_package, jsonData.importer_instance, jsonData.data).then(result =>{
db.disconnect().then(() =>{
zlib.gzip(JSON.stringify(result), (err, buffer) =>{
if (!err) {
ipc.of.pds.emit(buffer);
} else {
ipc.of.pds.emit(new Buffer('errror', 'utf-8'));
logger.error('Error', err);
}
});
});
});
} else {
logger.error('Error', err);
ipc.of.pds.emit(new Buffer('errror', 'utf-8'));
}
});
});
}
});
});
Client
On my machine (tested everything with the same node version as on the server)....
var responseDataLength = 0;
var responseBuffer = new Buffer(0);
zlib.gzip(JSON.stringify(toZip), (err, buffer) => {
ipc.serve(() =>{
ipc.server.on(
'data',
(data, socket) => {
// got client started confirmation, start comparing
var shutdownServer = () => {
ipc.server.emit(
socket,
new Buffer('shutdown', 'utf-8')
);
ipc.server.stop();
};
// convert only short messages to string, zipped data can stay in buffer form
var clientMessage = '';
if (data.length < 100) {
clientMessage = data.toString('utf-8');
}
//start
if (clientMessage === 'start') {
logger.debug('server sending buffer size', buffer.length);
ipc.server.emit(
socket,
new Buffer('hexMessageLength' + buffer.length, 'utf-8')
);
} else if (clientMessage === 'ack') {
logger.debug('got ack from client, server sending payload', buffer.length);
ipc.server.emit(
socket,
buffer
);
} else if (clientMessage.indexOf('hexResponseLength') > -1) {
responseDataLength = parseInt(clientMessage.substring('hexResponseLength'.length));
ipc.server.emit(
socket,
new Buffer('ack', 'utf-8')
);
} else if (clientMessage === 'error') {
// got an error, shutdown client and reject promise
shutdownServer();
defer.reject(err);
} else {
// data packets
responseBuffer = Buffer.concat([responseBuffer, data], responseBuffer.length + data.length);
// all done shutdown server and client and return the result
if (responseBuffer.length == responseDataLength) {
shutdownServer();
zlib.gunzip(responseBuffer, (err, buffer) =>{
defer.resolve(JSON.parse(buffer.toString('utf-8')));
});
}
}
}
);
});
ipc.server.start();
Server
var totalDataLength = 0;
var tempBuffer = new Buffer(0);
var processedData;
ipc.connectTo('pds', function (){
ipc.of.pds.on('connect', () =>{
logger.debug('connecting to server');
ipc.of.pds.emit(new Buffer('start', 'utf-8'));
});
ipc.of.pds.on('data', (data) =>{
var message = '';
if (data.length < 100) {
message = data.toString('utf-8');
}
if (message.indexOf('hexMessageLength') != -1) {
totalDataLength = parseInt(message.substring('hexMessageLength'.length));
ipc.of.pds.emit(
new Buffer('ack', 'utf-8')
);
} else if ('shutdown' === message) {
logger.debug('got shutdown message, kill the client');
process.exit();
} else if ('ack' === message) {
// server received response data length message, send actual buffered response data
ipc.of.pds.emit(processedData);
} else {
// combine data packets und unzip after all data was received
tempBuffer = Buffer.concat([tempBuffer, data], tempBuffer.length + data.length);
if (tempBuffer.length == totalDataLength) {
processData(tempBuffer).then((processResult) =>{
// data processing finished, send buffer size so that server knows what to except
processedData = processResult;
ipc.of.pds.emit(
new Buffer('hexResponseLength' + processResult.length, 'utf-8')
);
}).fail((err) =>{
logger.error(err);
ipc.of.pds.emit(new Buffer('error', 'utf-8'));
});
}
}
});
});
Client
https://github.com/mjurisic/viennajs-node-ipc
@jurisicmarko
mjurisic@gmail.com