11

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?

2
  • 2
    When I upgraded to Node.js v0.10.24, I no longer see the error: TypeError: Cannot set property 'maxSockets' of undefined Commented Mar 17, 2014 at 18:01
  • 1
    Note that Node 0.12 changed the default maxSockets value to Infinity: stackoverflow.com/a/12061013/504930 Commented Oct 6, 2015 at 14:59

1 Answer 1

14

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

Sign up to request clarification or add additional context in comments.

2 Comments

So, I doubled checked. It works fine on my local machine (node v0.10.3 and v0.8.7), but it crashes on my Joyent SmartOS machine (also running node v0.8.7) wit the message "TypeError: Cannot set property 'maxSockets' of undefined" Why would it be different?
It may be an architectural limitation of the Joyent PaaS. Address their support.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.