how to use http.Agent in node.js

本文详细解析了Node.js中Agent对象的maxSockets属性如何限制每个域名下的socket连接数。当新建请求时,若当前域名下的连接数未达到maxSockets设置的最大值,则创建新的socket;若已达到最大值,则将请求放入队列等待。

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

 

 1 Agent.prototype.addRequest = function(req, host, port) {
 2   var name = host + ':' + port;
 3   if (!this.sockets[name]) {
 4     this.sockets[name] = [];
 5   }
 6   if (this.sockets[name].length < this.maxSockets) {
 7     // If we are under maxSockets create a new one.
 8     req.onSocket(this.createSocket(name, host, port));
 9   } else {
10     // We are over limit so we'll add it to the queue.
11     if (!this.requests[name]) {
12       this.requests[name] = [];
13     }
14     this.requests[name].push(req);
15   }
16 };


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进来,看看有没有超,没超过,就加进去,超了,就先放放在这个域名下的队列里。

 

转载于:https://www.cnblogs.com/huenchao/p/6224794.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值