-
-
Save prabhakhar/2963290 to your computer and use it in GitHub Desktop.
var http = require('http'); | |
var options = { | |
host : 'www.nodejs.org', | |
port : 80, | |
method : 'HEAD', | |
path : '/api/http.html' | |
}; | |
for( var i = 0 ; i < 10000 ; ++i ) { | |
var req = http.request(options, function(res) { | |
}); | |
req.setTimeout(600, function() { | |
console.log('timed out'); | |
}); | |
req.end(); | |
} |
I guess the fix is just a matter of setTimeout removing the event listener when it fires?
@ronkorving - not sure if that helps as listeners accumulate with each call to setTimeout. It does not matter whether any request times out or not.
Link to the issue: nodejs/node-v0.x-archive#3505
Actually the above test does not demonstrate connection reuse!
I retract my first two comments. http.request
returns a new ClientRequest
each time - ClientRequest
is an EventEmitter - and hence timeout
listeners don't pile up when a connection is reused.
See https://gist.github.com/2973367 for a variant of the test. The leak warning comes up when a new request is created using a socket that has outstanding requests. You can verify this by setting the agent
to false
in which case the socket has no outstanding requests. This explains why this warning comes up under load with the default agent.
See https://gist.github.com/joyent/node/blob/v0.6.19-release/lib/net.js#L143.
To elaborate, EventEmitter.on is called for every setTimeout - so the listeners get piled up at https://github.com/joyent/node/blob/v0.6.19-release/lib/events.js#L160.