Skip to content

Instantly share code, notes, and snippets.

@Neilblaze
Created August 6, 2020 10:08
Show Gist options
  • Save Neilblaze/960e903a4d1cba7ab29b8efd9e85110c to your computer and use it in GitHub Desktop.
Save Neilblaze/960e903a4d1cba7ab29b8efd9e85110c to your computer and use it in GitHub Desktop.
NCM load-balancer // archived
const cluster = require('cluster')
const os = require('os')
if (cluster.isMaster) {
const cpus = os.cpus().length
console.log(`Forking for ${cpus} CPUs`)
for (let i = 0; i < cpus; i++) {
cluster.fork()
}
} else {
require('./server')
}
const http = require('http')
const pid = process.pid
http
.createServer((req, res) => {
for (let i = 0; i < 1e7; i++); // CPU work simulation
res.end(`Handled by process ${pid}`)
})
.listen(8080, () => {
console.log(`Started process ${pid}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment