Fix test-http-allow-req-after-204-res

Agent queue waits for connecting sockets.
This commit is contained in:
Ryan Dahl 2011-01-21 17:39:45 -08:00
parent 7dfbccf9bd
commit 4612b07604

View File

@ -951,11 +951,17 @@ Agent.prototype._establishNewConnection = function() {
// Grab a new "socket". Depending on the implementation of _getConnection // Grab a new "socket". Depending on the implementation of _getConnection
// this could either be a raw TCP socket or a TLS stream. // this could either be a raw TCP socket or a TLS stream.
var socket = this._getConnection(this.host, this.port, function () { var socket = this._getConnection(this.host, this.port, function () {
socket._httpConnecting = false;
self.emit('connect'); // mostly for the shim. self.emit('connect'); // mostly for the shim.
debug("Agent _getConnection callback"); debug("Agent _getConnection callback");
self._cycle(); 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); this.sockets.push(socket);
// Add a parser to the 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 // waiting sockets. If a waiting socket cannot be found, it will
// start the process of establishing one. // start the process of establishing one.
Agent.prototype._cycle = function() { Agent.prototype._cycle = function() {
debug("Agent _cycle"); debug("Agent _cycle sockets=" + this.sockets.length + " queue=" + this.queue.length);
var first = this.queue[0]; var first = this.queue[0];
if (!first) return; if (!first) return;
var haveConnectingSocket = false;
// First try to find an available socket. // First try to find an available socket.
for (var i = 0; i < this.sockets.length; i++) { for (var i = 0; i < this.sockets.length; i++) {
var socket = this.sockets[i]; var socket = this.sockets[i];
@ -1126,11 +1135,13 @@ Agent.prototype._cycle = function() {
first.assignSocket(socket); first.assignSocket(socket);
return; return;
} }
if (socket._httpConnecting) haveConnectingSocket = true;
} }
// Otherwise see if we should be starting a new connection to handle // If no sockets are connecting, and we have space for another we should
// this. // be starting a new connection to handle this request.
if (this.sockets.length < this.maxSockets) { if (!haveConnectingSocket && this.sockets.length < this.maxSockets) {
this._establishNewConnection(); this._establishNewConnection();
} }