简体   繁体   English

Node JS如何限制最大套接字数

[英]Node JS how to limit maximum number of sockets

is it possibile to set the maximum number of sockets that my server can handle? 可以设置服务器可以处理的最大套接字数量吗? Something like: maxSocket = 2 and from the 3 attempt of a socket to connect automatically refuse that connection? 像这样:maxSocket = 2并从3次尝试套接字连接时自动拒绝该连接?

Thanks 谢谢

In Node.js, you can set the maximum number of connections per origin. 在Node.js中,您可以设置每个起点的最大连接数。 If maxSockets is set, the low-level HTTP client queues requests and assigns them to sockets as they become available. 如果设置了maxSockets,则低级HTTP客户端会将请求排队,并在可用时将其分配给套接字。

Ref URL from AWS JS SDK 来自AWS JS SDK的引用URL

According to Node.js Doc , 根据Node.js Doc

By default set to Infinity. 默认情况下设置为“无限”。 Determines how many concurrent sockets the agent can have open per origin. 确定代理可以为每个源打开多少个并发套接字。 Origin is either a 'host:port' or 'host:port:localAddress' combination. 源是“主机:端口”或“主机:端口:本地地址”的组合。

Conventionally, they recommend 25~50, I guess. 我猜通常推荐25到50。

The default maxSockets value is Infinity and it determines the limit of number of sockets per host. maxSockets的默认值为Infinity ,它确定每个主机的套接字数限制。 See official docs: https://nodejs.org/api/http.html#http_agent_maxsockets 查看官方文档: https : //nodejs.org/api/http.html#http_agent_maxsockets

You can manually set maxSockets limit using the following code snippet 您可以使用以下代码段手动设置maxSockets限制

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

globalAgent : Global instance of Agent which is used as the default for all HTTP client requests. globalAgent :Agent的全局实例,用作所有HTTP客户端请求的默认实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM