Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion with cluster module when use "maxConnection" in its worker process #54882

Open
EchoFUN opened this issue Sep 11, 2024 · 4 comments
Open
Labels
cluster Issues and PRs related to the cluster subsystem. feature request Issues that request new features to be added to Node.js.

Comments

@EchoFUN
Copy link

EchoFUN commented Sep 11, 2024

What is the problem this feature will solve?

When the node service is under a very high load, multiple connections are processed at the same time in one worker ( we use the cluster module currently in our project ). We set the "maxConnections" to limit the connections of the worker. But we found that if a new request reach the limit of the "maxConnections", the request will retry on other workers. I think can we have an option, if a new request reach the limit, we can just drop the request instead of retrying the request on other workers ? Because as the system is under a very high load, the other workers may also be very busy at this moment. Here is a example on "v22.7.0".

const cluster = require('cluster');
const http = require('http');
const process = require('process');

if (cluster.isPrimary) {
  console.log(`Master ${process.pid} is running.\n`);
  for (let i = 0; i < 1; i++) {
    cluster.fork();
  }
} else {
  const server = http.createServer((req, res) => {
    res.writeHead(200);
    res.end('hello world\n');
  });

  server.maxConnections = 0;

  server.listen(8000, () => {
    console.log(`Worker ${process.pid} started`);
  });
}

What is the feature you are proposing to solve the problem?

For example, add An option "--maxconnections-drop-request" to the node "Command-line options" while on start up.

What alternatives have you considered?

No response

@EchoFUN EchoFUN added the feature request Issues that request new features to be added to Node.js. label Sep 11, 2024
@EchoFUN
Copy link
Author

EchoFUN commented Sep 11, 2024

The above sample code is used to better demonstrating this issue, it will cause the request to fail to return, and the CPU will run to the usage of 50%+.

@RedYetiDev RedYetiDev added the cluster Issues and PRs related to the cluster subsystem. label Sep 11, 2024
@theanarkh
Copy link
Contributor

I think it makes sense.

@EchoFUN
Copy link
Author

EchoFUN commented Sep 13, 2024

And on the documentation, it says "It is not recommended to use this option once a socket has been sent to a child with child_process.fork()." So, I can think of it as not recommended to use this API in cluster mode ? Is anyone have more detail information about it ? Thanks. It may be useful while the traffic is much heavy.

@theanarkh
Copy link
Contributor

I think It is not recommended to use this option in chld_process module.

If process A receives a socket, then process A's maxConnection will increase by one, and then you send it to process B, and process B's maxConnection will also increase by one, which may not be your expected behavior.

But I think it is ok if you close the socket of process A after sending successfully because the maxConnection will reduced by one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cluster Issues and PRs related to the cluster subsystem. feature request Issues that request new features to be added to Node.js.
Projects
Status: Awaiting Triage
Development

No branches or pull requests

3 participants