Skip to content

Instantly share code, notes, and snippets.

@cb-v4s
Last active July 16, 2021 21:21
Show Gist options
  • Select an option

  • Save cb-v4s/8fcef8da0c300d9d505a877d91f38d08 to your computer and use it in GitHub Desktop.

Select an option

Save cb-v4s/8fcef8da0c300d9d505a877d91f38d08 to your computer and use it in GitHub Desktop.
multi-core server manager for an express webserver performance
const cluster = require('cluster')
const express = require('express')
// const compression = require('compression')
const os = require('os')
const app = express()
const port = 3000
const ncpus = os.cpus().length
// compression middleware for smaller responses and better performance
// app.use(
// compression({
// level: 6,
// threshold: 10 * 10000,
// filter: (req, res) => {
// if (req.headers['x-no-compression']) return false;
// return compression.filter(req, res);
// }
// })
// )
app.get('/', (req, res) => {
res.send('Hello World!')
cluster.worker.kill()
})
if (cluster.isMaster){
for(i = 0; i < ncpus; i++){
cluster.fork()
}
// handle worker events
cluster.on('exit', (worker, code, signal) => {
cluster.fork()
})
} else {
app.listen(port, () => console.log(`Process ${process.pid} running @ http://localhost:${port}`))
}
// app.listen(port, () => console.log(`App running @ http://localhost:${port}`))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment