uNetworking / uWebSockets.js

μWebSockets for Node.js back-ends :metal:
Apache License 2.0
7.85k stars 570 forks source link

Cluster in Linux Debian [Solved] #467

Closed incapdns closed 3 years ago

incapdns commented 3 years ago

Error: Only one process is receiving requests, as the PID displayed in a request is the same for all connections Example: (Line 18) 1 request: PID 5839 2 request: PID 5839 3 request: PID 5839

Observation: This problem happens only with the uWebSockets.js module, when using the standard HTTP module of NodeJS or a c ++ addon module for NodeJS like Mitol or Hellepoll the load distribution works correctly

HTTP command: curl --no-keepalive http://127.0.0.1:1000

WS code:

const WebSocket = require('ws');

for(let i = 0; i < 5; i++){
    let ws = new WebSocket('ws://127.0.0.1:1000/test');

    ws.on('open', function () {
      ws.send('test')
    })

    ws.on('message', function (data) {
      console.log(data.toString());
    })
}

Linux version: Distributor ID: Debian Description: Debian GNU/Linux 9.13 (stretch) Release: 9.13 Codename: stretch

uws Server Code:

const cluster = require('cluster')
const numCPUs = require('os').cpus().length

function masterProc() {
    console.log(`Master ${process.pid} is running`);
    for (let i = 0; i < numCPUs; i++) {
        console.log(`Forking process number ${i}...`);
        cluster.fork();
    }
}

function childProc() {
    console.log(`Child PID: ${process.pid} running`);

    const app = require('uWebSockets.js').App()

    app.any("/*", res => {
      res.end('PID '+process.pid+'')
    }).listen(1000, () => {})
}

if (cluster.isMaster) {
  masterProc();
} else {
  childProc()
}

If debugging is necessary, I would like to request remote access to my computer via TeamViewer or Anydesk or another tool.

hst-m commented 3 years ago

I just tried this with Cluster and Worker Threads and it works on both using different workers, Depending on how you make requests to the server it can re-use the same connection/worker, I see browsers Chrome/Firefox doing this. You should switch between using 2 different browsers to get it to use different workers

incapdns commented 3 years ago

I have a doubt: I am running Debian through WSL (Windows Subsystem for Linux), and not a 100% Linux version (such as: Virtualbox or VMWare etc), could this be resulting in this problem?

I've tried using Firefox and even created a NodeJS application to generate a new connection (using request plugin) but the problem continues, so I think it may be related to WSL (Windows Subsystem for Linux)

Host System: Windows 10 x64

hst-m commented 3 years ago

I have a doubt: I am running Debian through WSL (Windows Subsystem for Linux), and not a 100% Linux version (such as: Virtualbox or VMWare etc), could this be resulting in this problem?

If you want to know for sure you should use curl to make requests to server, curl localhost:1000 or curl mydomain.com and it will alternate workers each request

incapdns commented 3 years ago

I have a doubt: I am running Debian through WSL (Windows Subsystem for Linux), and not a 100% Linux version (such as: Virtualbox or VMWare etc), could this be resulting in this problem?

If you want to know for sure you should use wget to make requests to server, use it like wget localhost:1000 or wget mydomain.com and it will alternate workers each request

Yes, plz verify: 1 Request: Google Chrome 2 Request: Firefox (Recent install + Anonymous tab) 3 Request: wget http://127.0.0.1:1000 4 Request: curl http://127.0.0.1:1000 image

hst-m commented 3 years ago

So looks like probably something up with WSL preventing that. I dont have WSL to try it. I use VirtualBox with Vagrant to automate stuff. You should probably be running the full VM and not the WSL to get more accurate virtual environment

incapdns commented 3 years ago

Yes, it really seems to be the case.

I tested it on a virtualized Linux server (using VirtualBox) and it worked 100%, The printscreen of the operation on the virtual machine below:

image

Problem solved !

But I'm curious because it doesn't work on WIndows or on Linux subversions like WSL

hst-m commented 3 years ago

A google search produces stuff like "WSL Network communication (although possible) requires several layers to pass through, making the WSL not ideal for low-level networking processes" and "Networking on WSL isn't fully reliable. WSL allows you to communicate with a network, but it may not be the best way to do so as it needs to pass through a few layers in order to do work"

A Virtual Machine runs a complete Linux Kernel. In contrast to WSL, the programs access the real kernel and drivers. When you use WSL the processes are scheduled by the Windows Scheduler. The network communications are processed by Windows. If you want to run or develop programs, using a virtual machine is a better option

incapdns commented 3 years ago

Got it, thanks anyway for helping.

I will be using uWebSockets.js for a VPS server application with the Centos 8 operating system, now that the question has been resolved, I feel more relaxed.

I will be closing the topic, thank you!

ghost commented 3 years ago

Multithreading is known to work: https://levelup.gitconnected.com/serving-100k-requests-second-from-a-fanless-raspberry-pi-4-over-ethernet-fdd2c2e05a1e