Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
571 views
in Technique[技术] by (71.8m points)

node.js - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...