|
|
@ -951,11 +951,17 @@ Agent.prototype._establishNewConnection = function() { |
|
|
|
// Grab a new "socket". Depending on the implementation of _getConnection
|
|
|
|
// this could either be a raw TCP socket or a TLS stream.
|
|
|
|
var socket = this._getConnection(this.host, this.port, function () { |
|
|
|
socket._httpConnecting = false; |
|
|
|
self.emit('connect'); // mostly for the shim.
|
|
|
|
debug("Agent _getConnection callback"); |
|
|
|
self._cycle(); |
|
|
|
}); |
|
|
|
|
|
|
|
// Use this special mark so that we know if the socket is connecting.
|
|
|
|
// TODO: come up with a standard way of specifying that a stream is being
|
|
|
|
// connected across tls and net.
|
|
|
|
socket._httpConnecting = true; |
|
|
|
|
|
|
|
this.sockets.push(socket); |
|
|
|
|
|
|
|
// Add a parser to the socket.
|
|
|
@ -1109,11 +1115,14 @@ Agent.prototype._getConnection = function(host, port, cb) { |
|
|
|
// waiting sockets. If a waiting socket cannot be found, it will
|
|
|
|
// start the process of establishing one.
|
|
|
|
Agent.prototype._cycle = function() { |
|
|
|
debug("Agent _cycle"); |
|
|
|
debug("Agent _cycle sockets=" + this.sockets.length + " queue=" + this.queue.length); |
|
|
|
|
|
|
|
var first = this.queue[0]; |
|
|
|
if (!first) return; |
|
|
|
|
|
|
|
|
|
|
|
var haveConnectingSocket = false; |
|
|
|
|
|
|
|
// First try to find an available socket.
|
|
|
|
for (var i = 0; i < this.sockets.length; i++) { |
|
|
|
var socket = this.sockets[i]; |
|
|
@ -1126,11 +1135,13 @@ Agent.prototype._cycle = function() { |
|
|
|
first.assignSocket(socket); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (socket._httpConnecting) haveConnectingSocket = true; |
|
|
|
} |
|
|
|
|
|
|
|
// Otherwise see if we should be starting a new connection to handle
|
|
|
|
// this.
|
|
|
|
if (this.sockets.length < this.maxSockets) { |
|
|
|
// If no sockets are connecting, and we have space for another we should
|
|
|
|
// be starting a new connection to handle this request.
|
|
|
|
if (!haveConnectingSocket && this.sockets.length < this.maxSockets) { |
|
|
|
this._establishNewConnection(); |
|
|
|
} |
|
|
|
|
|
|
|