Node.js http.Agent实现真正的keep-Alive代理

一、 Kepp-Alive使用情况

1、当你的Server内存充足时,KeepAlive =On还是Off对系统性能影响不大。

2、当你的Server上静态网页(Html、图片、Css、Js)居多时,建议打开KeepAlive 。

3、当你的Server多为动态请求(因为连接数据库,对文件系统访问较多),KeepAlive 关掉,会节省一定的内存,节省的内存正好可以作为文件系统的Cache(vmstat命令中cache一列),降低I/O压力。 

PS:当KeepAlive =On时,KeepAliveTimeOut的设置其实也是一个问题,设置的过短,会导致Apache 频繁建立连接,给Cpu造成压力,设置的过长,系统中就会堆积无用的Http连接,消耗掉大量内存,具体设置多少,可以进行不断的调节,因你的网站浏览和服务器配置 而异。


二、浏览器对Keep-Alive的并发数情况及域名开销

对于HTTP/1.0来说可以充分利用浏览器默认最大并发连接数比HTTP/1.1多的好 处,实现不增加新域名的开销而更高的并行下载,减少域名解释的开销(注:IE 6,7在HTTP/1.0中默认最大并发连接数为4,在HTTP/1.1中默认最大并发连接数为2,IE8都为6,Firefox2在HTTP/1.0中 默认最大并发连接数为2 在HTTP/1.1中默认最大并发连接数为8,firefox 3默认都是6),根据10年7月Google索引的42亿个网页的统计报告,每张网页里包含29.39个图片,7.09个外部脚本,3.22个外部CSS 样式表,如果设置了Keep-Alive并且合理控制Keep-Alive TimeOut这个参数可以大量的节约连接的开销,提高相应速度。如果设置不好,在大并发的情况小,因维持大量连接而使服务器资源耗尽,而对于目前国内大 部分的用户使用的还是IE6,7的情况


三、使用Node.js的http.Agent开发一个真正的Keep-Alive代理

/**
 * Created by Administrator on 14-4-30.
 */
var http = require('http');
var EventEmitter = require('evnets').EventEmitter;
var net = require('net');
var util = require('util');

var Agent = function(optinos)
{
    var self = this;
    // 选项配置
    self.options  = options || {};
    // 保存请求的全部hostname
    self.requests = {};
    // 创建的socket连接数
    self.sockets = {};
    // 未被使用的socket
    self.unusedSockets = {};
    // socket的最大连接数量
    self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets;
    self.on('free',function(socket,host,port){
        var hostname = host + ':' + port;
        // 如果有正在请求的主机
        if(self.requests[hostname] && self.requests[hostname].length)
        {
            self.requests[hostname].shift().onSocket(socket);
        }
        else
        {
            // 如果没有请求数就销毁socket连接并从连接池移除
            if(!self.unusedSockets[hostname])
            {
                self.unusedSockets[hostname] = [];
            }
            self.unusedSockets[hostname].push(socket);
        }
    });
    self.createConnection = net.createConnection;
};
util.inherits(Agent,EventEmitter);

Agent.defaultMaxSockets = 10;

Agent.prototype.defaultPort = 80;

Agent.prototype.addRequest = function(req,host,port)
{
    var hostname = host + ':' + port;
    if(this.unusedSockets[hostname] && this.unusedSockets[hostname].length)
    {
        req.onSocket(this.unusedSockets[hostname].shift());
        return;
    }
    if(!this.sockets[hostname])
    {
        this.sockets[hostname] = [];
    }
    if(this.sockets[hostname].length < this.maxSockets)
    {
        req.onSocket(this.createSocket(hostname,host,port));
    }
    else
    {
        if(!this.requests[hostname])
        {
            this.requests[hostname] = [];
        }
        this.requests[hostname].push(req);
    }
};

Agent.prototype.createSocket = function(name, host, port) {
    var self = this;
    var s = self.createConnection(port, host, self.options);
    if (!self.sockets[name]) {
        self.sockets[name] = [];
    }
    this.sockets[name].push(s);
    var onFree = function() {
        self.emit('free', s, host, port);
    }
    s.on('free', onFree);
    var onClose = function(err) {
        // 这是唯一移除socket代理的地方,如果你想从连接池中移除socket,就关闭连接,所有的socket错误会导致连接关闭
        self.removeSocket(s, name, host, port);
    }
    s.on('close', onClose);
    var onRemove = function() {
        // We need this function for cases like HTTP "upgrade"
        // (defined by WebSockets) where we need to remove a socket from the pool
        //  because it'll be locked up indefinitely
        self.removeSocket(s, name, host, port);
        s.removeListener('close', onClose);
        s.removeListener('free', onFree);
        s.removeListener('agentRemove', onRemove);
    }
    s.on('agentRemove', onRemove);
    return s;
};
Agent.prototype.removeSocket = function(s, name, host, port) {
    if (this.sockets[name]) {
        var index = this.sockets[name].indexOf(s);
        if (index !== -1) {
            this.sockets[name].splice(index, 1);
        }
    } else if (this.sockets[name] && this.sockets[name].length === 0) {
        delete this.sockets[name];
        delete this.requests[name];
    }
    if (this.requests[name] && this.requests[name].length) {
        // If we have pending requests and a socket gets closed a new one
        // needs to be created to take over in the pool for the one that closed.
        this.createSocket(name, host, port).emit('free');
    }
};


如何使用:

var http = require('http');
var keepAliveAgent = require('./agent.js'); 
var agent = new keepAliveAgent({ maxSockets: 100 }); 
// Optionally define more parallel sockets 
var options = {
    agent: agent,
    hostname: 'example.com',
    path: '/path'}; 
// do get
http.get(options);
// do request
http.request(options,function(){});


转载于:https://my.oschina.net/antianlu/blog/261105

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值