Browse Source

http: agent should cycle on close

v0.7.4-release
Ryan Dahl 14 years ago
parent
commit
68f2aa2715
  1. 14
      lib/http.js
  2. 33
      test/simple/test-http-agent.js

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

33
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);
});
Loading…
Cancel
Save