Nodejs 最大套接字池设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16472497/
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
Nodejs Max Socket Pooling Settings
提问by Justin Cloud
So, I am trying to optimize my node application and my app makes HTTP and HTTPS requests.
因此,我正在尝试优化我的节点应用程序,并且我的应用程序发出 HTTP 和 HTTPS 请求。
From this article from LinkedInfor making node fast it suggests disabling socket pooling to remove the limit of 5 sockets:
从LinkedIn 的这篇文章中可以提高节点速度,它建议禁用套接字池以消除 5 个套接字的限制:
// Disable socket pooling
var http = require('http');
var options = {.....};
options.agent = false;
var req = http.request(options)
Now from Mikeal (the developer of Request) on GitHub, he suggests:
现在来自 GitHub 上的 Mikeal(Request 的开发者),他建议:
require('http').globalAgent.maxSockets = Infinity
To be fair, he doesn't suggest infinity, but you could put any reasonable value there.
公平地说,他不建议无穷大,但你可以在那里放置任何合理的值。
Now, my app uses http and https, so I used this code:
现在,我的应用程序使用 http 和 https,所以我使用了以下代码:
var http = require('http');
http.globalAgent.maxSockets = 30;
var https = require('https');
https.globalAgent.maxSockets = 30;
When I do this, I get this error:
当我这样做时,我收到此错误:
TypeError: Cannot set property 'maxSockets' of undefined
类型错误:无法设置未定义的属性“maxSockets”
Finally, in looking at the HTTP document it doesn't show a "globalAgent", but instead shows just agent.maxSockets.
最后,在查看 HTTP 文档时,它没有显示“globalAgent”,而是仅显示agent.maxSockets。
So, I am wondering first, what is the best syntax for overriding this parameter?
所以,我首先想知道,覆盖此参数的最佳语法是什么?
Second, what is an optimal value? Is it based on the amount of memory my server has? Its bandwidth?
其次,什么是最优值?它是否基于我的服务器拥有的内存量?它的带宽?
回答by josh3736
In regard to the TypeErroryou're getting, I don't get any errors when setting either http.globalAgent.maxSocketsor https.globalAgent.maxSockets. There's something else going on in your app.
关于TypeError你得到的,在设置http.globalAgent.maxSockets或时我没有收到任何错误https.globalAgent.maxSockets。您的应用程序中还有其他事情发生。
Regarding the first part of your question, realize that you're not just restricted to using the global agent. You can create your own Agentinstances and use that to make requests:
关于问题的第一部分,请意识到您不仅限于使用全局代理。您可以创建自己的Agent实例并使用它来发出请求:
var http = require('http');
var myAgent = new http.Agent();
http.request({ ... , agent: myAgent }, ...);
Requests made using custom agents don't interact with the global agent at all. The global agent is just the default one that gets used if you don't explicitly specify one or opt-out of using agents all together (by passing falseas the agentvalue in the request options).
使用自定义代理发出的请求根本不与全局代理交互。如果您没有明确指定一个或选择不使用所有代理(通过false作为agent请求选项中的值传递),则全局代理只是默认使用的代理。
So when the docs say agent.maxSockets, they're really referring to the generic Agentclass; every instance has that property, including the global (default) agent – which you mustaccess through http.globalAgent.
因此,当文档说 时agent.maxSockets,他们实际上指的是泛型Agent类;每个实例都具有该属性,包括全局(默认)代理——您必须通过http.globalAgent.
The second part of your question (optimal maxSockets) is a tough one to answer. Remember that many servers will limit the number of concurrent connections from a given IP, and you want to make sure that you don't overwhelm a server with a large number of concurrent requests. (With enough requests fired off at once, you're esentially DOSing the server.)
您问题的第二部分(最佳maxSockets)很难回答。请记住,许多服务器会限制来自给定 IP 的并发连接数,并且您要确保不会因大量并发请求而使服务器不堪重负。(一次发出足够多的请求,您基本上是在对服务器进行 DOS 操作。)

