Node.js 最大并发连接数为 1000

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17033631/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:45:21  来源:igfitidea点击:

Node.js maxing out at 1000 concurrent connections

node.jsamazon-web-services

提问by jadent

We're benchmarking node performance using a simple Hello World node server on AWS (EC2).

我们正在 AWS (EC2) 上使用一个简单的 Hello World 节点服务器对节点性能进行基准测试。

No matter what size instance we use Node always appears to max out at 1000 concurrent connections (this is NOT 1000 per second, but 1000 it can handle at 1 time). Shortly after that the CPU spikes and node basically freezes.

无论我们使用什么大小的实例,Node 总是出现 1000 个并发连接的最大值(这不是每秒 1000 个,而是它可以在 1 次处理 1000 个)。不久之后,CPU 出现峰值,节点基本冻结。

Node v0.10.5

节点 v0.10.5

var http = require('http');
var server = http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('loaderio-dec86f35bc8ba1b9b604db6c328864c1');
});
server.maxHeadersCount = 0;
server.listen(4000);

Node should be able to handle more than this correct? Any thoughts would be greatly appreciated.

节点应该能够处理比这更多的正确吗?任何想法将不胜感激。

Also the file descriptors (soft, hard, system) are set to 65096)

文件描述符(软、硬、系统)也设置为 65096)

采纳答案by Daniel

Use the posixmodule to raise the limit on the number of file descriptors your process can use.

使用该posix模块来提高进程可以使用的文件描述符数量的限制。

Install posix

安装 posix

npm install posix

Then in your code that runs when you launch your app...

然后在您启动应用程序时运行的代码中...

var posix = require('posix');

// raise maximum number of open file descriptors to 10k,
// hard limit is left unchanged
posix.setrlimit('nofile', { soft: 10000 });

回答by BadCanyon

You've reached the default limit of file descriptors a process can use (1024). You can check the limit on the command line by running "ulimit -n". To change the limit, you need to edit /etc/security/limits.conf. Add the follow block:

您已达到进程可以使用的文件描述符的默认限制 (1024)。您可以通过运行“ulimit -n”在命令行上检查限制。要更改限制,您需要编辑 /etc/security/limits.conf。添加以下块:

* soft nofile 65535
* hard nofile 65535
root soft nofile 65535
root hard nofile 65535

"*" applies to all users except root. Limits for root must be added separately. There are soft and hard limit. Users are allowed to change their own limits up to the soft limit but not exceeding the hard limit.

“*”适用于除 root 之外的所有用户。必须单独添加 root 的限制。有软限制和硬限制。用户可以将自己的限制更改为软限制,但不能超过硬限制。

Once the file has been edited, log out and back in again. Verify the change by running ulimit -n. Restart your Node process and you should be good to go.

编辑完文件后,注销并重新登录。通过运行 ulimit -n 验证更改。重新启动您的 Node 进程,您应该一切顺利。

There's also a system-wide file descriptor limit that can be increased with the following command:

还有一个系统范围的文件描述符限制,可以使用以下命令增加:

sysctl -w fs.file-max=65535