nodejs Agent does not drop the connection after timeout

Viewed 279

I want to configure the http.Agent to drop the connection after 5 seconds but it never happens.

The server is configured to drop the connection after 20 seconds (for this test)

The connection is dropped after 20 seconds (server drops the connection istead of the client)

var http = require('http');

var myAgent = new http.Agent({keepAlive:true, keepAliveMsecs:5000});

var client = http.get({host:'localhost', agent:myAgent}, function(incMsg){
    incMsg.on('data', function(chunk){ console.log(chunk.toString()) });
    incMsg.on('end', function(){ console.log('-- END OF MESSAGE --') });    
});

How can I configure the Agent to drop the connection for its http clients ?

I'm fairly new in http server client connections so sorry if I missed something obvious but I can't google it out

1 Answers

agent option timeout:5000 must be used instead of the keepAliveMsecs:5000

here is the working code

var http = require('http');

var myAgent = new http.Agent({keepAlive:true, timeout:5000});

var client = http.get({host:'localhost', agent:myAgent}, function(incMsg){
    incMsg.on('data', function(chunk){ console.log(chunk.toString()) });
    incMsg.on('end', function(){ console.log('-- END OF MESSAGE --') });    
});
Related