Skip to content

Commit 9947129

Browse files
committed
Added cluster server for testing
1 parent 05b0ee7 commit 9947129

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

cluster.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict'
2+
3+
const cluster = require('cluster')
4+
const http = require('http')
5+
const numCPUs = Math.floor(require('os').cpus().length / 2) || 1
6+
7+
if (cluster.isMaster) {
8+
console.log(`Master ${process.pid} is running`)
9+
10+
for (let i = 0; i < numCPUs; i++) {
11+
cluster.fork()
12+
}
13+
14+
cluster.on('exit', (worker, code, signal) => {
15+
console.log(`worker ${worker.process.pid} died`)
16+
})
17+
} else {
18+
// Workers can share any TCP connection
19+
// In this case it is an HTTP server
20+
http.createServer((req, res) => {
21+
res.writeHead(200)
22+
res.end('hello world\n')
23+
}).listen(3000)
24+
25+
console.log(`Worker ${process.pid} started`)
26+
}

0 commit comments

Comments
 (0)