1 module.exports = ForeverAgent
2 ForeverAgent.SSL = ForeverAgentSSL
4 var util = require('util')
5 , Agent = require('http').Agent
8 , AgentSSL = require('https').Agent
10 function getConnectionName(host, port) {
12 if (typeof host === 'string') {
13 name = host + ':' + port
15 // For node.js v012.0 and iojs-v1.5.1, host is an object. And any existing localAddress is part of the connection name.
16 name = host.host + ':' + host.port + ':' + (host.localAddress ? (host.localAddress + ':') : ':')
21 function ForeverAgent(options) {
23 self.options = options || {}
27 self.maxSockets = self.options.maxSockets || Agent.defaultMaxSockets
28 self.minSockets = self.options.minSockets || ForeverAgent.defaultMinSockets
29 self.on('free', function(socket, host, port) {
30 var name = getConnectionName(host, port)
32 if (self.requests[name] && self.requests[name].length) {
33 self.requests[name].shift().onSocket(socket)
34 } else if (self.sockets[name].length < self.minSockets) {
35 if (!self.freeSockets[name]) self.freeSockets[name] = []
36 self.freeSockets[name].push(socket)
38 // if an error happens while we don't use the socket anyway, meh, throw the socket away
39 var onIdleError = function() {
42 socket._onIdleError = onIdleError
43 socket.on('error', onIdleError)
45 // If there are no pending requests just destroy the
46 // socket and it will get removed from the pool. This
47 // gets us out of timeout issues and allows us to
48 // default to Connection:keep-alive.
54 util.inherits(ForeverAgent, Agent)
56 ForeverAgent.defaultMinSockets = 5
59 ForeverAgent.prototype.createConnection = net.createConnection
60 ForeverAgent.prototype.addRequestNoreuse = Agent.prototype.addRequest
61 ForeverAgent.prototype.addRequest = function(req, host, port) {
62 var name = getConnectionName(host, port)
64 if (typeof host !== 'string') {
70 if (this.freeSockets[name] && this.freeSockets[name].length > 0 && !req.useChunkedEncodingByDefault) {
71 var idleSocket = this.freeSockets[name].pop()
72 idleSocket.removeListener('error', idleSocket._onIdleError)
73 delete idleSocket._onIdleError
74 req._reusedSocket = true
75 req.onSocket(idleSocket)
77 this.addRequestNoreuse(req, host, port)
81 ForeverAgent.prototype.removeSocket = function(s, name, host, port) {
82 if (this.sockets[name]) {
83 var index = this.sockets[name].indexOf(s)
85 this.sockets[name].splice(index, 1)
87 } else if (this.sockets[name] && this.sockets[name].length === 0) {
89 delete this.sockets[name]
90 delete this.requests[name]
93 if (this.freeSockets[name]) {
94 var index = this.freeSockets[name].indexOf(s)
96 this.freeSockets[name].splice(index, 1)
97 if (this.freeSockets[name].length === 0) {
98 delete this.freeSockets[name]
103 if (this.requests[name] && this.requests[name].length) {
104 // If we have pending requests and a socket gets closed a new one
105 // needs to be created to take over in the pool for the one that closed.
106 this.createSocket(name, host, port).emit('free')
110 function ForeverAgentSSL (options) {
111 ForeverAgent.call(this, options)
113 util.inherits(ForeverAgentSSL, ForeverAgent)
115 ForeverAgentSSL.prototype.createConnection = createConnectionSSL
116 ForeverAgentSSL.prototype.addRequestNoreuse = AgentSSL.prototype.addRequest
118 function createConnectionSSL (port, host, options) {
119 if (typeof port === 'object') {
121 } else if (typeof host === 'object') {
123 } else if (typeof options === 'object') {
129 if (typeof port === 'number') {
133 if (typeof host === 'string') {
137 return tls.connect(options);