@@ -951,11 +951,17 @@ Agent.prototype._establishNewConnection = function() {
951951 // Grab a new "socket". Depending on the implementation of _getConnection
952952 // this could either be a raw TCP socket or a TLS stream.
953953 var socket = this . _getConnection ( this . host , this . port , function ( ) {
954+ socket . _httpConnecting = false ;
954955 self . emit ( 'connect' ) ; // mostly for the shim.
955956 debug ( "Agent _getConnection callback" ) ;
956957 self . _cycle ( ) ;
957958 } ) ;
958959
960+ // Use this special mark so that we know if the socket is connecting.
961+ // TODO: come up with a standard way of specifying that a stream is being
962+ // connected across tls and net.
963+ socket . _httpConnecting = true ;
964+
959965 this . sockets . push ( socket ) ;
960966
961967 // Add a parser to the socket.
@@ -1109,11 +1115,14 @@ Agent.prototype._getConnection = function(host, port, cb) {
11091115// waiting sockets. If a waiting socket cannot be found, it will
11101116// start the process of establishing one.
11111117Agent . prototype . _cycle = function ( ) {
1112- debug ( "Agent _cycle" ) ;
1118+ debug ( "Agent _cycle sockets=" + this . sockets . length + " queue=" + this . queue . length ) ;
11131119
11141120 var first = this . queue [ 0 ] ;
11151121 if ( ! first ) return ;
11161122
1123+
1124+ var haveConnectingSocket = false ;
1125+
11171126 // First try to find an available socket.
11181127 for ( var i = 0 ; i < this . sockets . length ; i ++ ) {
11191128 var socket = this . sockets [ i ] ;
@@ -1126,11 +1135,13 @@ Agent.prototype._cycle = function() {
11261135 first . assignSocket ( socket ) ;
11271136 return ;
11281137 }
1138+
1139+ if ( socket . _httpConnecting ) haveConnectingSocket = true ;
11291140 }
11301141
1131- // Otherwise see if we should be starting a new connection to handle
1132- // this.
1133- if ( this . sockets . length < this . maxSockets ) {
1142+ // If no sockets are connecting, and we have space for another we should
1143+ // be starting a new connection to handle this request .
1144+ if ( ! haveConnectingSocket && this . sockets . length < this . maxSockets ) {
11341145 this . _establishNewConnection ( ) ;
11351146 }
11361147
0 commit comments