如何在Node.js中设置最大http客户端连接 [英] How to set maximum http client connections in Node.js

查看:152
本文介绍了如何在Node.js中设置最大http客户端连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要增加我正在编写的模块可用的出站http套接字的数量。我知道可以使用以下方式执行全球

I need to increase the number of outbound http sockets usable by a module I am writing. I know it is possible to do this globally using:

// increase max sockets
http.globalAgent.maxSockets = 1000;

在不影响其他模块的情况下,创建模块的maxSocket限制的最佳模式是什么?

What is the best pattern for authoring a module's maxSocket limit without affecting other modules?

推荐答案

您最好的选择是将Agent对象传递给您正在进行的每个http和https请求,并根据您的喜好配置maxSockets。这对其他模块没有影响。
请参阅 http://nodejs.org/docs/latest/api/ http.html#http_http_request_options_callback

Your best option is to pass an Agent object to every http and https request you are making, with maxSockets configured to your liking. This will have no effect on other modules. See http://nodejs.org/docs/latest/api/http.html#http_http_request_options_callback.

如果您自己使用框架,而不是直接依赖http模块,则可能很难并需要一些修补。

If you are using a framework yourself, rather than relying on the http module directly, it might be difficult and require some patching.

这篇关于如何在Node.js中设置最大http客户端连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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