|
@ -66,6 +66,7 @@ function Agent(options) { |
|
|
}); |
|
|
}); |
|
|
self.createConnection = net.createConnection; |
|
|
self.createConnection = net.createConnection; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
util.inherits(Agent, EventEmitter); |
|
|
util.inherits(Agent, EventEmitter); |
|
|
exports.Agent = Agent; |
|
|
exports.Agent = Agent; |
|
|
|
|
|
|
|
@ -91,6 +92,7 @@ Agent.prototype.addRequest = function(req, host, port, localAddress) { |
|
|
this.requests[name].push(req); |
|
|
this.requests[name].push(req); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
Agent.prototype.createSocket = function(name, host, port, localAddress, req) { |
|
|
Agent.prototype.createSocket = function(name, host, port, localAddress, req) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
var options = util._extend({}, self.options); |
|
|
var options = util._extend({}, self.options); |
|
@ -111,18 +113,21 @@ Agent.prototype.createSocket = function(name, host, port, localAddress, req) { |
|
|
self.sockets[name] = []; |
|
|
self.sockets[name] = []; |
|
|
} |
|
|
} |
|
|
this.sockets[name].push(s); |
|
|
this.sockets[name].push(s); |
|
|
var onFree = function() { |
|
|
|
|
|
|
|
|
function onFree() { |
|
|
self.emit('free', s, host, port, localAddress); |
|
|
self.emit('free', s, host, port, localAddress); |
|
|
} |
|
|
} |
|
|
s.on('free', onFree); |
|
|
s.on('free', onFree); |
|
|
var onClose = function(err) { |
|
|
|
|
|
|
|
|
function onClose(err) { |
|
|
// This is the only place where sockets get removed from the Agent.
|
|
|
// This is the only place where sockets get removed from the Agent.
|
|
|
// If you want to remove a socket from the pool, just close it.
|
|
|
// If you want to remove a socket from the pool, just close it.
|
|
|
// All socket errors end in a close event anyway.
|
|
|
// All socket errors end in a close event anyway.
|
|
|
self.removeSocket(s, name, host, port, localAddress); |
|
|
self.removeSocket(s, name, host, port, localAddress); |
|
|
} |
|
|
} |
|
|
s.on('close', onClose); |
|
|
s.on('close', onClose); |
|
|
var onRemove = function() { |
|
|
|
|
|
|
|
|
function onRemove() { |
|
|
// We need this function for cases like HTTP 'upgrade'
|
|
|
// We need this function for cases like HTTP 'upgrade'
|
|
|
// (defined by WebSockets) where we need to remove a socket from the pool
|
|
|
// (defined by WebSockets) where we need to remove a socket from the pool
|
|
|
// because it'll be locked up indefinitely
|
|
|
// because it'll be locked up indefinitely
|
|
@ -134,6 +139,7 @@ Agent.prototype.createSocket = function(name, host, port, localAddress, req) { |
|
|
s.on('agentRemove', onRemove); |
|
|
s.on('agentRemove', onRemove); |
|
|
return s; |
|
|
return s; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
Agent.prototype.removeSocket = function(s, name, host, port, localAddress) { |
|
|
Agent.prototype.removeSocket = function(s, name, host, port, localAddress) { |
|
|
if (this.sockets[name]) { |
|
|
if (this.sockets[name]) { |
|
|
var index = this.sockets[name].indexOf(s); |
|
|
var index = this.sockets[name].indexOf(s); |
|
|