Nodejs Max Socket Pooling Settings

前端 未结 1 1741
星月不相逢
星月不相逢 2020-12-14 04:19

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

From this article from LinkedIn for making node fast it suggests disabling

相关标签:
1条回答
  • 2020-12-14 04:55

    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.

    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 }, ...);
    

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

    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.

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

    0 讨论(0)
提交回复
热议问题