Clustered Threading cluster_threads module like cluster module that supports sharing (or different ports) a port for listening #48350
Clustering of threads
, basically similar to clustered thread pools with features same as Cluster module
.
Clustered threads module
: The module Clustered threads module
which is basically Clustering of threads
will be similar to clustered thread pools with features same as inbuilt Cluster module
based on child_process.fork() features with minor differences.
Clustering of threads
, basically similar to Cluster module
where each thread
based on specification can:
listen
tosame port
listen
todifferent ports
nodejs module: cluster
cluster
-
The worker processes are spawned using the child_process.fork() method, so that they can communicate with the parent via IPC and pass server handles back and forth.
-
The cluster module supports two methods of distributing incoming connections.
- The
first one
(and the default one on all platforms except Windows) is theround-robin approach
, where the primary process listens on a port - The
second approach
is where the primary process creates the listen socket andsends it to interested workers
.
- The
-
Because
server.listen()
hands off
most
of thework to
theprimary process
, there are three cases where the behavior between a normalNode.js process and a cluster worker differs
:a.)
server.listen({fd: 7})
Because the message is passed to the primary,file descriptor 7
in the parentwill be listened on
, and thehandle passed to the worker
, rather than listening to the worker's idea of what the number 7 file descriptor references.b.)
server.listen(handle)
Listening on handles explicitly will cause theworker to use the supplied handle
, rather than talk to the primary process.c.)
server.listen(0)
Normally, thiswill cause servers
tolisten on a random port
. However, in a cluster,each worker will receive the same "random" port
each time they do listen(0). In essence, the port is random the first time, but predictable thereafter.To listen on a unique port, generate a port number based on the cluster worker ID
.
shares the same port
process.same.port.js:
const cluster = require('node:cluster');
const http = require('node:http');
const numCPUs = require('node:os').availableParallelism();
const process = require('node:process');
if (cluster.isPrimary) {
console.log(`Primary ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
console.log(`Worker ${process.pid} started`);
}
shares a different port
process.different.port.js:
const cluster = require('node:cluster');
const http = require('node:http');
const numCPUs = require('node:os').availableParallelism();
const process = require('node:process');
if (cluster.isPrimary) {
console.log(`Primary ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(process.pid);
console.log(`Worker ${process.pid} started`);
}
shares the same port
:
const cluster_thread = require('node:cluster_thread');
const http = require('node:http');
const numCPUs = require('node:os').availableParallelism();
const process = require('node:process');
let workers = [];
if (cluster_thread.isPrimary) {
console.log(`Primary ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
workers[i] = new cluster_thread.Worker(__filename, {
workerData: script,
});
}
workers[i].on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(8000);
console.log(`Worker ${process.pid}, ${cluster_thread.worker.threadId} started`);
}
shares a different port
:
const cluster_thread = require('node:cluster_thread');
const http = require('node:http');
const numCPUs = require('node:os').availableParallelism();
const process = require('node:process');
let workers = [];
if (cluster_thread.isMainThread) {
console.log(`Primary ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
workers[i] = new cluster_thread.Worker(__filename, {
workerData: script,
});
}
workers[i].on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
http.createServer((req, res) => {
res.writeHead(200);
res.end('hello world\n');
}).listen(cluster_thread.worker.threadId);
console.log(`Worker ${process.pid}, ${cluster_thread.worker.threadId} started`);
}