From 68f2aa2715dbfda6a9ec4c007458ef66421dd6c1 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Fri, 21 Jan 2011 14:53:31 -0800 Subject: [PATCH] http: agent should cycle on close --- lib/http.js | 14 ++++++++++++-- test/simple/test-http-agent.js | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 test/simple/test-http-agent.js diff --git a/lib/http.js b/lib/http.js index 17b8f00cac2..d70c9cfcd13 100644 --- a/lib/http.js +++ b/lib/http.js @@ -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(); }); diff --git a/test/simple/test-http-agent.js b/test/simple/test-http-agent.js new file mode 100644 index 00000000000..5e12d2e2c67 --- /dev/null +++ b/test/simple/test-http-agent.js @@ -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); +});