一、当你的Server内存充足时,KeepAlive =On仍是Off对系统性能影响不大。css
二、当你的Server上静态网页(Html、图片、Css、Js)居多时,建议打开KeepAlive 。数据库
三、当你的Server多为动态请求(由于链接数据库,对文件系统访问较多),KeepAlive 关掉,会节省必定的内存,节省的内存正好能够做为文件系统的Cache(vmstat命令中cache一列),下降I/O压力。 浏览器
PS:当KeepAlive =On时,KeepAliveTimeOut的设置其实也是一个问题,设置的太短,会致使Apache 频繁创建链接,给Cpu形成压力,设置的过长,系统中就会堆积无用的Http链接,消耗掉大量内存,具体设置多少,能够进行不断的调节,因你的网站浏览和服务器配置 而异。服务器
对于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的状况并发
/** * 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'); } };
如何使用:socket
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(){});