Skip to content

Instantly share code, notes, and snippets.

@tibawatanabe
Last active September 9, 2018 01:47
Show Gist options
  • Save tibawatanabe/823486816ea7af5b93b9fb1ca53b1bc7 to your computer and use it in GitHub Desktop.
Save tibawatanabe/823486816ea7af5b93b9fb1ca53b1bc7 to your computer and use it in GitHub Desktop.
Node.js cluster example
const cluster = require('cluster');
if (cluster.isMaster) {
// Count the machine's CPUs
let cpuCount = require('os').cpus().length;
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
startWorker();
}
} else {
startApp();
}
function startWorker() {
cluster.fork();
}
function startApp() {
const app = new App();
app.listen(process.env.PORT, function () {
console.log('App started.');
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment