Skip to content

Commit

Permalink
Multi core
Browse files Browse the repository at this point in the history
  • Loading branch information
hakanensari committed Sep 18, 2024
1 parent 34abcbb commit 3bf6565
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
const app = require("./app")
const cluster = require("cluster")
const os = require("os")

const port = process.env.PORT || 3000
app.listen(port, () => {
console.log(`Listening on port ${port}`)
})
if (cluster.isMaster) {
// Fork workers
for (let i = 0; i < os.cpus().length; i++) {
cluster.fork()
}

cluster.on("exit", (worker, code, signal) => {
console.log(`Worker ${worker.process.pid} died`)
})
} else {
const app = require("./app")

const port = process.env.PORT || 3000
app.listen(port, () => {
console.log(`Worker ${cluster.worker.id} listening on port ${port}`)
})
}

0 comments on commit 3bf6565

Please sign in to comment.