Last active
September 9, 2018 01:47
-
-
Save tibawatanabe/823486816ea7af5b93b9fb1ca53b1bc7 to your computer and use it in GitHub Desktop.
Node.js cluster example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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