mirror of https://github.com/nodejs/node.git
node cluster is now a module instead of CLI interface
This is to make room for master process plugins instead of adding CLI options as proposed in #1879.v0.7.4-release
parent
4fb2ac5be1
commit
c5d54010bc
|
@ -4,14 +4,22 @@ A single instance of Node runs in a single thread. To take advantage of
|
||||||
multi-core systems the user will sometimes want to launch a cluster of Node
|
multi-core systems the user will sometimes want to launch a cluster of Node
|
||||||
processes to handle the load.
|
processes to handle the load.
|
||||||
|
|
||||||
By starting node with the `cluster` argument, Node will detect the number of
|
The cluster module allows you to easily create a network of processes all
|
||||||
CPUs on the machine and start that many processes. For example suppose we
|
which share server ports.
|
||||||
had a simple HTTP server in server.js:
|
|
||||||
|
|
||||||
require('http').createServer(function(req, res) {
|
var cluster = require('cluster');
|
||||||
|
var http = require('http');
|
||||||
|
|
||||||
|
if (!cluster.isWorker()) {
|
||||||
|
// Start the master process, fork workers.
|
||||||
|
cluster.startMaster({ workers: 2 });
|
||||||
|
} else {
|
||||||
|
// Worker processes have a http server.
|
||||||
|
http.Server(function(req, res) {
|
||||||
res.writeHead(200);
|
res.writeHead(200);
|
||||||
res.end('hello world\n');
|
res.end("hello world\n");
|
||||||
}).listen(8000);
|
}).listen(8000);
|
||||||
|
}
|
||||||
|
|
||||||
If we start it like this
|
If we start it like this
|
||||||
|
|
||||||
|
|
|
@ -48,22 +48,47 @@ var queryIds = 0;
|
||||||
var queryCallbacks = {};
|
var queryCallbacks = {};
|
||||||
|
|
||||||
|
|
||||||
exports.start = function() {
|
|
||||||
|
// Used to check if this process is a worker or not.
|
||||||
|
// Returns boolean.
|
||||||
|
exports.isWorker = function() {
|
||||||
|
return 'NODE_WORKER_ID' in process.env;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
// Call this from the master process. It will start child workers.
|
||||||
|
//
|
||||||
|
// options.workerFilename
|
||||||
|
// Specifies the script to execute for the child processes. Default is
|
||||||
|
// process.argv[1]
|
||||||
|
//
|
||||||
|
// options.args
|
||||||
|
// Specifies program arguments for the workers. The Default is
|
||||||
|
// process.argv.slice(2)
|
||||||
|
//
|
||||||
|
// options.workers
|
||||||
|
// The number of workers to start. Defaults to os.cpus().length.
|
||||||
|
exports.startMaster = function(options) {
|
||||||
amMaster = true;
|
amMaster = true;
|
||||||
|
|
||||||
if (process.argv.length < 1) {
|
if (!options) {
|
||||||
console.error('Usage: node cluster script.js');
|
options = {};
|
||||||
process.exit(1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var args = process.argv.slice(2);
|
if (!options.workerFilename) {
|
||||||
var scriptFilename = args.shift();
|
options.workerFilename = process.argv[1];
|
||||||
|
}
|
||||||
|
|
||||||
var cpus = require('os').cpus().length;
|
if (!options.args) {
|
||||||
console.error("Detected " + cpus + " cpus");
|
options.args = process.argv.slice(2);
|
||||||
|
}
|
||||||
|
|
||||||
for (var i = 0; i < cpus; i++) {
|
if (!options.workers) {
|
||||||
forkWorker(scriptFilename, args);
|
options.workers = require('os').cpus().length;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < options.workers; i++) {
|
||||||
|
forkWorker(options.workerFilename, options.args);
|
||||||
}
|
}
|
||||||
|
|
||||||
process.on('uncaughtException', function(e) {
|
process.on('uncaughtException', function(e) {
|
||||||
|
@ -118,7 +143,7 @@ function handleWorkerMessage(worker, message) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function forkWorker(scriptFilename, args) {
|
function forkWorker(workerFilename, args) {
|
||||||
var id = ++ids;
|
var id = ++ids;
|
||||||
var envCopy = {};
|
var envCopy = {};
|
||||||
|
|
||||||
|
@ -128,7 +153,7 @@ function forkWorker(scriptFilename, args) {
|
||||||
|
|
||||||
envCopy['NODE_WORKER_ID'] = id;
|
envCopy['NODE_WORKER_ID'] = id;
|
||||||
|
|
||||||
var worker = fork(scriptFilename, args, {
|
var worker = fork(workerFilename, args, {
|
||||||
env: envCopy
|
env: envCopy
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -2261,7 +2261,6 @@ static void ParseDebugOpt(const char* arg) {
|
||||||
static void PrintHelp() {
|
static void PrintHelp() {
|
||||||
printf("Usage: node [options] [ -e script | script.js ] [arguments] \n"
|
printf("Usage: node [options] [ -e script | script.js ] [arguments] \n"
|
||||||
" node debug script.js [arguments] \n"
|
" node debug script.js [arguments] \n"
|
||||||
" node cluster script.js [arguments] \n"
|
|
||||||
"\n"
|
"\n"
|
||||||
"Options:\n"
|
"Options:\n"
|
||||||
" -v, --version print node's version\n"
|
" -v, --version print node's version\n"
|
||||||
|
|
|
@ -68,10 +68,6 @@
|
||||||
var d = NativeModule.require('_debugger');
|
var d = NativeModule.require('_debugger');
|
||||||
d.start();
|
d.start();
|
||||||
|
|
||||||
} else if (process.argv[1] == 'cluster') {
|
|
||||||
var cluster = NativeModule.require('cluster');
|
|
||||||
cluster.start();
|
|
||||||
|
|
||||||
} else if (process._eval != null) {
|
} else if (process._eval != null) {
|
||||||
// User passed '-e' or '--eval' arguments to Node.
|
// User passed '-e' or '--eval' arguments to Node.
|
||||||
var Module = NativeModule.require('module');
|
var Module = NativeModule.require('module');
|
||||||
|
|
Loading…
Reference in New Issue