How to set maximum http client connections in Node.js

后端 未结 2 1861
栀梦
栀梦 2021-01-05 02:07

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:

//         


        
2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-05 02:31

    Given that the setting determines how many concurrent connections are available per host, there is no way to set this specifically for individual modules. What you should do however is make sure that you're not reducing the number of concurrent connections from what the user has already specified. For example, if someone sets their maxSockets to 5000, and you set it to 1000 in your module, bad things could happen. I would recommend something like the following

    var max = 1000;
    if(http.globalAgent.maxSockets < max) {
        http.globalAgent.maxSockets = max;
    }
    

提交回复
热议问题