Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nodejs Max Socket Pooling Settings

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

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.

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: Cannot set property 'maxSockets' of undefined

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?

like image 595
Justin Cloud Avatar asked May 09 '13 22:05

Justin Cloud


1 Answers

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

like image 191
josh3736 Avatar answered Sep 28 '22 00:09

josh3736