Open ganeshkbhat opened 11 months ago
Is this not just a duplicate of https://github.com/oven-sh/bun/issues/2428 ?
@melroy89 No this #2428 is for cluster_threads
[multi-threading] not cluster
multi-processing that implements clustering or the child_processes
What is the problem this feature would solve?
Clustering of threads
, basicallysimilar to clustered thread pools with features same as Cluster module
.https://github.com/ganeshkbhat/loadbalancer/blob/4f15c79434797ccaf17aeae6f6a3f4c47f0e5703/nodejs.proposal.md
I am recommend implementing
cluster_thread
module which is genuinely a good starting point for creating clusters of threads like clusters of processes in node.js. Currently a multi-threading option is a little buggy. There is no random round robin as well in Windows due to absence offile descriptor
module [presumed - check] in windows environment.This is the Node.js Suggestion recommendation. https://github.com/nodejs/node/issues/48350
What is the feature you are proposing to solve the problem?
Clustered threads module
: The moduleClustered threads module
which is basicallyClustering of threads
will besimilar 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 toCluster module
whereeach thread
based on specification can:listen
tosame port
listen
todifferent ports
Background:
nodejs module: cluster
clusterThe 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.
first one
(and the default one on all platforms except Windows) is theround-robin approach
, where the primary process listens on a portsecond approach
is where the primary process creates the listen socket andsends it to interested workers
.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
.CHILD PROCESS PORT SHARING:
shares the same port
process.same.port.js: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
); }RECOMMENDED AND EXPECTED WORKER THREAD CLUSTER MODULE AND PORT SHARING like cluster module:
shares the same port
:shares a different port
: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
); }