Permalink
Browse files

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

Agent queue waits for connecting sockets.
  • Loading branch information...
1 parent 7dfbccf commit 4612b07604bf654bc521dd0473f82b166deb7412 @ry ry committed Jan 22, 2011
Showing with 15 additions and 4 deletions.
  1. +15 −4 lib/http.js
View
@@ -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();
}

0 comments on commit 4612b07

Please sign in to comment.