Nodejs 最大套接字池设置 [英] Nodejs Max Socket Pooling Settings

查看:93
本文介绍了Nodejs 最大套接字池设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试优化我的节点应用程序,并且我的应用程序发出 HTTP 和 HTTPS 请求.

So, I am trying to optimize my node application and my app makes HTTP and HTTPS requests.

来自 LinkedIn 的这篇文章 为了使节点更快,它建议禁用套接字池以消除 5 个套接字的限制:

From this article from LinkedIn for making node fast it suggests disabling socket pooling to remove the limit of 5 sockets:

// Disable socket pooling
var http = require('http');
var options = {.....};
options.agent = false;
var req = http.request(options)

现在来自 GitHub 上的 Mikeal(Request 的开发者),他建议:

Now from Mikeal (the developer of Request) on GitHub, he suggests:

require('http').globalAgent.maxSockets = Infinity

公平地说,他不建议无穷大,但你可以在那里放置任何合理的值.

To be fair, he doesn't suggest infinity, but you could put any reasonable value there.

现在,我的应用程序使用 http 和 https,所以我使用了以下代码:

Now, my app uses http and https, so I used this code:

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: 无法设置未定义的属性 'maxSockets'

TypeError: Cannot set property 'maxSockets' of undefined

最后,在查看 HTTP 文档时,它没有显示globalAgent",而是仅显示 agent.maxSockets.

Finally, in looking at the HTTP document it doesn't show a "globalAgent", but instead shows just 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?

推荐答案

关于你得到的 TypeError,我在设置 http.globalAgent 时都没有收到任何错误.maxSocketshttps.globalAgent.maxSockets.您的应用中还有其他事情要做.

In regard to the TypeError you're getting, I don't get any errors when setting either http.globalAgent.maxSockets or https.globalAgent.maxSockets. There's something else going on in your app.

关于您问题的第一部分,请意识到您不仅限于使用全局代理.您可以创建自己的 Agent 实例并使用它来发出请求:

Regarding the first part of your question, realize that you're not just restricted to using the global agent. You can create your own Agent instances and use that to make requests:

var http = require('http');
var myAgent = new http.Agent();

http.request({ ... , agent: myAgent }, ...);

使用自定义代理发出的请求根本不会与全局代理交互.如果您没有明确指定一个或选择不使用所有代理(通过将 false 作为 agent 值传递),全局代理只是默认使用的代理在请求选项中).

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 false as the agent value in the request options).

所以当文档说 agent.maxSockets 时,他们实际上指的是通用的 Agent 类;每个实例都具有该属性,包括全局(默认)代理您必须通过http.globalAgent访问.

So when the docs say agent.maxSockets, they're really referring to the generic Agent class; every instance has that property, including the global (default) agent – which you must access through http.globalAgent.

您问题的第二部分(最佳maxSockets)很难回答.请记住,许多服务器会限制来自给定 IP 的并发连接数,并且您要确保不会因大量并发请求而使服务器不堪重负.(一次发出足够多的请求,您基本上是在对服务器进行 DOS 攻击.)

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.)

这篇关于Nodejs 最大套接字池设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆