-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster.js
More file actions
35 lines (28 loc) · 819 Bytes
/
Copy pathcluster.js
File metadata and controls
35 lines (28 loc) · 819 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import cluster from "cluster"
import http from "http"
import { cpus } from "os"
import process from "process"
import { setupMaster } from "@socket.io/sticky"
import { start } from "./index.js"
const WORKERS_COUNT = 10
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`)
for (let i = 0; i < WORKERS_COUNT; i++) {
cluster.fork()
}
cluster.on("exit", (worker) => {
console.log(`Worker ${worker.process.pid} died!`)
cluster.fork()
})
const httpServer = http.createServer()
setupMaster(httpServer, {
loadBalancingMethod: "least-connection", // random, robin-robin
})
const PORT = process.env.PORT || 4000
httpServer.listen(PORT, () => {
console.log(`Server listening at port${PORT}`)
})
} else {
console.log(`Worker ${process.pid} started`)
start()
}