-
Hello, follow up of my message on #214 I just want to know how to make it work because when I done that: import cluster from "cluster";
import { cpus } from "os";
const numCPUs = cpus().length;
function masterProcess() {
console.log(`Master ${process.pid} is running`);
for (let i = 0; i < numCPUs; i++) {
console.log(`Forking process number ${i}...`);
cluster.fork();
}
}
function childProcess() {
console.log(`Worker ${process.pid} started...`);
app
.any("/*", res => {
/* Let's deny all Http */
res.end("Nothing to see here! (" + process.pid + ")");
})
.listen(serverPort, listenSocket => {
if (listenSocket) {
log(`Listening to port ${serverPort}`);
}
});
}
if (cluster.isMaster) {
masterProcess();
} else {
childProcess();
} I always get the same PID, I don't know why, I just tested with a simple httpServer include with node and it works, so can it really works like that? Sorry for my noobiness :x |
Beta Was this translation helpful? Give feedback.
Replies: 9 comments 2 replies
-
I get different pid. You probably get the same because your browser keeps the same connection. Try with curl and you'll see different pids. |
Beta Was this translation helpful? Give feedback.
-
I've already test it.... so i'ts my computer who give me always the same because is not "stressed" enough to load balance to another? |
Beta Was this translation helpful? Give feedback.
-
Sorry to comment again but is the |
Beta Was this translation helpful? Give feedback.
-
Sorry @alexhultman , but can I have an answer on my last question? |
Beta Was this translation helpful? Give feedback.
-
Everything has to be in child, the entire app should be isolated per process or per thread |
Beta Was this translation helpful? Give feedback.
-
Yes I see now why, great thanks, but I really don't know how to do this with a clean architecture since I have a lot of routes http and ws |
Beta Was this translation helpful? Give feedback.
-
I got the same PID with a similar code in macOS Catalina. but I got the different PID in ubuntu 16.04. |
Beta Was this translation helpful? Give feedback.
-
Clustering is a Linux feature, it does not work on Windows or macOS. You'll get different PIDs when you stress the server on Linux. |
Beta Was this translation helpful? Give feedback.
-
@alexhultman forking the process several times in order to "create a cluster" is pretty common in nodejs, it works on all platforms, on Linux and macOS TCP sockets are shared across forked instances, it works with standard tcp sockets / express and other frameworks, is there a reason why it doesn't work with uWebSockets.js? Does it need to be implemented in the native module? |
Beta Was this translation helpful? Give feedback.
Clustering is a Linux feature, it does not work on Windows or macOS. You'll get different PIDs when you stress the server on Linux.