Skip to content

Commit

Permalink
http: agent should cycle on close
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Jan 21, 2011
1 parent 4927552 commit 68f2aa2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,8 @@ Agent.prototype._establishNewConnection = function() {
self._removeSocket(socket);
// unref the parser for easy gc
parsers.free(parser);

self._cycle();
});

parser.onIncoming = function(res, shouldKeepAlive) {
Expand All @@ -1068,13 +1070,21 @@ Agent.prototype._establishNewConnection = function() {
}

res.addListener('end', function() {
debug('AGENT request complete disconnecting.');
debug('AGENT request complete');
// For the moment we reconnect for every request. FIXME!
// All that should be required for keep-alive is to not reconnect,
// but outgoingFlush instead.
if (!req.shouldKeepAlive) socket.end();
if (!req.shouldKeepAlive) {
debug('AGENT socket.end()');
socket.end();
} else {
debug('AGENT socket keep-alive');
}

req.detachSocket(socket);

assert(!socket._httpMessage);

self._cycle();
});

Expand Down
33 changes: 33 additions & 0 deletions test/simple/test-http-agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var common = require('../common');
var assert = require('assert');
var http = require('http');

var server = http.Server(function(req, res) {
res.writeHead(200);
res.end("hello world\n");
});

var responses = 0;
var N = 10;
var M = 10;

server.listen(common.PORT, function() {
for (var i = 0; i < N; i++) {
setTimeout(function () {
for (var j = 0; j < M; j++) {
http.get({ port: common.PORT, path: '/', }, function(res) {
console.log(res.statusCode);
if (++responses == N * M) server.close();
}).on('error', function(e) {
console.log(e.message);
process.exit(1);
});
}
}, i);
}
});


process.on('exit', function() {
assert.equal(N * M, responses);
});

0 comments on commit 68f2aa2

Please sign in to comment.