Node.js 中的“连接 EMFILE”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10355501/
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
"connect EMFILE" error in Node.js
提问by codekick
I have very recently received a lot of traffic to my site that runs Node.js. With the increasing traffic it has started to crash a lot, which has not happened before. I get the following error in my log:
我最近在运行 Node.js 的网站上收到了大量流量。随着交通量的增加,它开始崩溃很多,这是以前从未发生过的。我的日志中出现以下错误:
{ [Error: connect EMFILE] code: 'EMFILE', errno: 'EMFILE', syscall: 'connect' }
Error: connect EMFILE
at errnoException (net.js:670:11)
at connect (net.js:548:19)
at net.js:607:9
at Array.0 (dns.js:88:18)
at EventEmitter._tickCallback (node.js:192:40)
Anyone that have an idea why it crash? And ideas how to solve it?
任何人都知道它为什么会崩溃?以及如何解决的想法?
I'm using Express.js and Socket.io. It runs on Ubuntu.
我正在使用 Express.js 和 Socket.io。它在 Ubuntu 上运行。
采纳答案by stewe
EMFILEerror means that the OS is denying your program to open more files/sockets.
EMFILE错误意味着操作系统拒绝您的程序打开更多文件/套接字。
Have a look at: How do I change the number of open files limit in Linux?
回答by GauravLuthra
EMFILEmeans error maximum fileswhich indicates that the OS is denying your program to open more file descriptors.
EMFILE手段error maximum files这表明OS否认你的程序打开更多的文件描述符。
Note that open files in the system include disk files, named pipes, network sockets and devices opened by all processes.
请注意,系统中打开的文件包括所有进程打开的磁盘文件、命名管道、网络套接字和设备。
Your operating system specifies the open file limit per process.
您的操作系统指定了每个进程的打开文件限制。
To check open file limit of your system use ulimit -acommand. Generally on unix like system it is default to 1024.
要检查系统使用ulimit -a命令的打开文件限制。通常在类 Unix 系统上它默认为 1024。
If the limitation was 1024, means you/process can open maximum 1024 files. if you exceed this limit means open, pipeand dupsystem calls will fail and yield EMFILE error.
如果限制为 1024,则意味着您/进程最多可以打开 1024 个文件。如果超出此限制的手段open,pipe和dup系统调用将失败,并产生EMFILE错误。
When you get EMFILEit mostly indicates the leak in your code. Increasing the ulimit to a higher value is not a good solution in case there is leak in your program.
当你得到EMFILE它时,它主要表明你的代码中存在泄漏。如果您的程序存在泄漏,将 ulimit 增加到更高的值并不是一个好的解决方案。
You should try to find out the cause of leak. Following can be used to debug in unix like operating system :
您应该尝试找出泄漏的原因。以下可用于在类似 Unix 的操作系统中进行调试:
lsofmeaninglist open filescommand gives the list of open files. Assume your nodeJS program is having leak so to find out total number of file descriptors opened by your program :lsof | grep node | wc -l
To find out the file descriptors for sockets, use:
lsof -n -i -P | grep node
lsof含义list open files命令给出打开文件的列表。假设您的 nodeJS 程序有泄漏,以便找出您的程序打开的文件描述符总数:lsof | grep 节点 | wc -l
要找出套接字的文件描述符,请使用:
lsof -n -i -P | grep 节点
Using this we can find out where the leak is and then we can correct it.
使用它我们可以找出泄漏的位置,然后我们可以纠正它。
回答by Michael Benin
In my case, we already had the ulimit set and kern.maxfiles configuration highest it could go on a Mac.
就我而言,我们已经设置了 ulimit 和 kern.maxfiles 配置,它可以在 Mac 上运行。
What fixed it was limiting the number of max sockets globally. In later versions of node the limit is Infinity.
修复它的是限制全局最大套接字的数量。在更高版本的节点中,限制为无穷大。
Try adding these lines of code:
尝试添加这些代码行:
var https = require('https');
var http = require('http');
https.globalAgent.maxSockets = 5;
http.globalAgent.maxSockets = 5;
In the case of using the request library, you can configure these settings.
在使用请求库的情况下,您可以配置这些设置。
See: https://github.com/request/request#request---simplified-http-client
见:https: //github.com/request/request#request---simplified-http-client
Here is more on maxSockets: https://nodejs.org/api/http.html#http_agent_maxsockets
这里有更多关于 maxSockets 的内容:https://nodejs.org/api/http.html#http_agent_maxsockets

