| 1 | | /*! |
| 2 | | * agentkeepalive - lib/agent.js |
| 3 | | * |
| 4 | | * refer: |
| 5 | | * * @atimb "Real keep-alive HTTP agent": https://gist.github.com/2963672 |
| 6 | | * * https://github.com/joyent/node/blob/master/lib/http.js |
| 7 | | * |
| 8 | | * Copyright(c) 2012 fengmk2 <fengmk2@gmail.com> |
| 9 | | * MIT Licensed |
| 10 | | */ |
| 11 | | |
| 12 | 1 | "use strict"; |
| 13 | | |
| 14 | | /** |
| 15 | | * Module dependencies. |
| 16 | | */ |
| 17 | | |
| 18 | 1 | var http = require('http'); |
| 19 | 1 | var https = require('https'); |
| 20 | 1 | var util = require('util'); |
| 21 | | |
| 22 | 1 | var debug; |
| 23 | 1 | if (process.env.NODE_DEBUG && /agentkeepalive/.test(process.env.NODE_DEBUG)) { |
| 24 | 0 | debug = function (x) { |
| 25 | 0 | console.error('agentkeepalive:', x); |
| 26 | | }; |
| 27 | | } else { |
| 28 | 1 | debug = function () { }; |
| 29 | | } |
| 30 | | |
| 31 | 1 | function Agent(options) { |
| 32 | 12 | options = options || {}; |
| 33 | 12 | http.Agent.call(this, options); |
| 34 | | |
| 35 | 12 | var self = this; |
| 36 | | // max requests per keepalive socket, default is 0, no limit. |
| 37 | 12 | self.maxKeepAliveRequests = parseInt(options.maxKeepAliveRequests, 10) || 0; |
| 38 | | // max keep alive time, default 60 seconds. |
| 39 | | // if set `maxKeepAliveTime = 0`, will disable keepalive feature. |
| 40 | 12 | self.maxKeepAliveTime = parseInt(options.maxKeepAliveTime, 10); |
| 41 | 12 | if (isNaN(self.maxKeepAliveTime)) { |
| 42 | 9 | self.maxKeepAliveTime = 60000; |
| 43 | | } |
| 44 | 12 | self.unusedSockets = {}; |
| 45 | 12 | self.createSocketCount = 0; |
| 46 | 12 | self.timeoutSocketCount = 0; |
| 47 | 12 | self.requestFinishedCount = 0; |
| 48 | | |
| 49 | | // override the `free` event listener |
| 50 | 12 | self.removeAllListeners('free'); |
| 51 | 12 | self.on('free', function (socket, host, port, localAddress) { |
| 52 | 27 | self.requestFinishedCount++; |
| 53 | 27 | socket._requestCount++; |
| 54 | 27 | var name = host + ':' + port; |
| 55 | 27 | if (localAddress) { |
| 56 | 0 | name += ':' + localAddress; |
| 57 | | } |
| 58 | 27 | if (self.requests[name] && self.requests[name].length > 0) { |
| 59 | 12 | self.requests[name].shift().onSocket(socket); |
| 60 | 12 | if (self.requests[name].length === 0) { |
| 61 | | // don't leak |
| 62 | 4 | delete self.requests[name]; |
| 63 | | } |
| 64 | | } else { |
| 65 | | // If there are no pending requests just destroy the |
| 66 | | // socket and it will get removed from the pool. This |
| 67 | | // gets us out of timeout issues and allows us to |
| 68 | | // default to Connection:keep-alive. |
| 69 | | // socket.destroy(); |
| 70 | 15 | if (self.maxKeepAliveTime === 0 || |
| 71 | | (self.maxKeepAliveRequests && socket._requestCount >= self.maxKeepAliveRequests)) { |
| 72 | 4 | socket.destroy(); |
| 73 | 4 | return; |
| 74 | | } |
| 75 | | |
| 76 | | // Avoid duplicitive timeout events by removing timeout listeners set on |
| 77 | | // socket by previous requests. node does not do this normally because it |
| 78 | | // assumes sockets are too short-lived for it to matter. It becomes a |
| 79 | | // problem when sockets are being reused. Steps are being taken to fix |
| 80 | | // this issue upstream in node v0.10.0. |
| 81 | | // |
| 82 | | // See https://github.com/joyent/node/commit/451ff1540ab536237e8d751d241d7fc3391a4087 |
| 83 | 11 | if (self.maxKeepAliveTime && socket._events && Array.isArray(socket._events.timeout)) { |
| 84 | 1 | socket.removeAllListeners('timeout'); |
| 85 | | // Restore the socket's setTimeout() that was remove as collateral |
| 86 | | // damage. |
| 87 | 1 | socket.setTimeout(self.maxKeepAliveTime, socket._maxKeepAliveTimeout); |
| 88 | | } |
| 89 | | // keepalive |
| 90 | 11 | if (!self.unusedSockets[name]) { |
| 91 | 6 | self.unusedSockets[name] = []; |
| 92 | | } |
| 93 | 11 | self.unusedSockets[name].push(socket); |
| 94 | | } |
| 95 | | }); |
| 96 | | } |
| 97 | | |
| 98 | 1 | util.inherits(Agent, http.Agent); |
| 99 | 1 | module.exports = Agent; |
| 100 | | |
| 101 | 1 | Agent.prototype.addRequest = function (req, host, port, localAddress) { |
| 102 | 27 | var name = host + ':' + port; |
| 103 | 27 | if (localAddress) { |
| 104 | 0 | name += ':' + localAddress; |
| 105 | | } |
| 106 | 27 | if (this.unusedSockets[name] && this.unusedSockets[name].length > 0) { |
| 107 | 5 | return req.onSocket(this.unusedSockets[name].shift()); |
| 108 | | } |
| 109 | 22 | return http.Agent.prototype.addRequest.call(this, req, host, port, localAddress); |
| 110 | | }; |
| 111 | | |
| 112 | 1 | Agent.prototype.createSocket = function (name, host, port, localAddress, req) { |
| 113 | 12 | var self = this; |
| 114 | 12 | var socket = http.Agent.prototype.createSocket.call(this, name, host, port, localAddress, req); |
| 115 | 12 | socket._requestCount = 0; |
| 116 | 12 | if (self.maxKeepAliveTime) { |
| 117 | 11 | socket._maxKeepAliveTimeout = function () { |
| 118 | 1 | socket.destroy(); |
| 119 | 1 | self.timeoutSocketCount++; |
| 120 | | }; |
| 121 | 11 | socket.setTimeout(self.maxKeepAliveTime, socket._maxKeepAliveTimeout); |
| 122 | | // Disable Nagle's algorithm: http://blog.caustik.com/2012/04/08/scaling-node-js-to-100k-concurrent-connections/ |
| 123 | 11 | socket.setNoDelay(true); |
| 124 | | } |
| 125 | 12 | this.createSocketCount++; |
| 126 | 12 | return socket; |
| 127 | | }; |
| 128 | | |
| 129 | 1 | Agent.prototype.removeSocket = function (socket, name, host, port, localAddress) { |
| 130 | 8 | if (this.unusedSockets[name]) { |
| 131 | 4 | var unusedIndex = this.unusedSockets[name].indexOf(socket); |
| 132 | 4 | if (unusedIndex !== -1) { |
| 133 | 2 | this.unusedSockets[name].splice(unusedIndex, 1); |
| 134 | 2 | if (this.unusedSockets[name].length === 0) { |
| 135 | | // don't leak |
| 136 | 2 | delete this.unusedSockets[name]; |
| 137 | | } |
| 138 | | } |
| 139 | | } |
| 140 | 8 | return http.Agent.prototype.removeSocket.call(this, socket, name, host, port, localAddress); |
| 141 | | }; |
| 142 | | |
| 143 | 1 | function HttpsAgent(options) { |
| 144 | 1 | Agent.call(this, options); |
| 145 | 1 | this.createConnection = https.globalAgent.createConnection; |
| 146 | | } |
| 147 | 1 | util.inherits(HttpsAgent, Agent); |
| 148 | 1 | HttpsAgent.prototype.defaultPort = 443; |
| 149 | | |
| 150 | 1 | Agent.HttpsAgent = HttpsAgent; |