Skip to content

Instantly share code, notes, and snippets.

@caesaneer
Last active September 21, 2023 20:47
Show Gist options
  • Save caesaneer/417cd88062d3bebcfaf8708ebc17862e to your computer and use it in GitHub Desktop.
Save caesaneer/417cd88062d3bebcfaf8708ebc17862e to your computer and use it in GitHub Desktop.
Go Goroutines vs Node.js Cluster & Worker Threads - Part 1 - Node.js Code - CLUSTER
const cluster = require('cluster')
const http = require('http')
const numCPUs = require('os').cpus().length
const host = '192.168.0.14'
const port = 3000
const start = async function startServer() {
// Cluster
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running.`)
// Run cluster.fork based on numCPUs
for (let i = 0; i < numCPUs; i += 1) {
cluster.fork()
}
cluster.on('exit', (worker, code, signal) => {
console.log(`A worker with ID ${worker.process.pid} died.`)
})
} else {
// Simple request router
const router = function requestRouter(request, reply) {
reply.end('OK')
}
// Start HTTP server
const server = http.createServer(router)
server.listen(port, host, () => {
console.log(`Node.js Standard Library HTTP server running on port: ${port}`)
})
}
}
start()
@melroy89
Copy link

re-test with bun maybe now :)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment