how to use http.Agent in node.js

时间:2024-07-09 21:36:32

Actually now that I look at the Agent code, it looks like it sets maxSockets on a per domain basis in an Agent:

 Agent.prototype.addRequest = function(req, host, port) {
var name = host + ':' + port;
if (!this.sockets[name]) {
this.sockets[name] = [];
}
if (this.sockets[name].length < this.maxSockets) {
// If we are under maxSockets create a new one.
req.onSocket(this.createSocket(name, host, port));
} else {
// We are over limit so we'll add it to the queue.
if (!this.requests[name]) {
this.requests[name] = [];
}
this.requests[name].push(req);
}
};

What is the expected behavior of maxSockets on an Agent?  Should maxSockets represent the total number of sockets available to that agent, or the total number of sockets available to each host:port in that agent?  My vote is for the former, since it's possible to build the later on top of it, but not vice versa.

上面的意思就是如果你在代理对象上设置了 maxSockets 这个属性,那对于每一个域名下的 sockets 数量就应该≤ 这个数目,有新的req进来,看看有没有超,没超过,就加进去,超了,就先放放在这个域名下的队列里。