http: agent should cycle on close

This commit is contained in:
Ryan Dahl 2011-01-21 14:53:31 -08:00
parent 49275524a5
commit 68f2aa2715
2 changed files with 45 additions and 2 deletions

View File

@ -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) {
@ -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();
});

View File

@ -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);
});