Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send consecutive requests with HTTP keep-alive in node.js?

I'm using node.js 0.6.18, and the following code makes node.js close the TCP connection between every two requests (verified with strace on Linux). How do I make node.js reuse the same TCP connection for multiple HTTP requests (i.e. keep-alive)? Please note that the webserver is capable of keep-alive, it works with other clients. The webserver returns a chunked HTTP response.

var http = require('http');
var cookie = 'FOO=bar';
function work() {
  var options = {
      host: '127.0.0.1',
      port: 3333,
      path: '/',
      method: 'GET',
      headers: {Cookie: cookie},
  };
  process.stderr.write('.')
  var req = http.request(options, function(res) {
    if (res.statusCode != 200) {
      console.log('STATUS: ' + res.statusCode);
      console.log('HEADERS: ' + JSON.stringify(res.headers));
      process.exit(1)
    }
    res.setEncoding('utf8');
    res.on('data', function (chunk) {});
    res.on('end', function () { work(); });
  });
  req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
    process.exit(1);
  });
  req.end();
}
work()
like image 959
pts Avatar asked Jun 05 '12 10:06

pts


People also ask

How do I keep my HTTP request alive?

The Keep-Alive general header allows the sender to hint about how the connection may be used to set a timeout and a maximum amount of requests. Note: Set the Connection header to "keep-alive" for this header to have any effect.

CAN node js handle multiple requests?

How NodeJS handle multiple client requests? NodeJS receives multiple client requests and places them into EventQueue. NodeJS is built with the concept of event-driven architecture. NodeJS has its own EventLoop which is an infinite loop that receives requests and processes them.

How many concurrent HTTP requests can node js handle?

js can handle ~15K requests per second, and the vanilla HTTP module can handle 70K rps.

What is http keep alive timeout?

The keep alive timeout on the Message Processor allows a single TCP connection to send and receive multiple HTTP requests/responses from/to the backend server, instead of opening a new connection for every request/response pair.


2 Answers

I was able to get this to work (verified with strace) by creating an http.Agent and setting its maxSockets property to 1. I don't know if this is the ideal way to do it; however, it does meet the requirements. One thing that I did notice is that what the docs claimed about http.Agent behavior did not accurately describe how it worked in practice. Code below:

var http = require('http');
var cookie = 'FOO=bar';
var agent = new http.Agent;
agent.maxSockets = 1;

function work() {
  var options = {
      host: '127.0.0.1',
      port: 3000,
      path: '/',
      method: 'GET',
      headers: {Cookie: cookie},
      agent: agent
  };
  process.stderr.write('.')
  var req = http.request(options, function(res) {
    if (res.statusCode != 200) {
      console.log('STATUS: ' + res.statusCode);
      console.log('HEADERS: ' + JSON.stringify(res.headers));
      process.exit(1)
    }
    res.setEncoding('utf8');
    res.on('data', function (chunk) {});
    res.on('end', function () { work(); });
  });
  req.on('error', function(e) {
    console.log('problem with request: ' + e.message);
    process.exit(1);
  });
  req.end();
}
work()

EDIT: I should add that I did my testing with node.js v0.8.7

like image 57
Charles Hooper Avatar answered Oct 03 '22 00:10

Charles Hooper


you can just set:

http.globalAgent.keepAlive = true
like image 29
Krishna Srinivas Avatar answered Oct 03 '22 01:10

Krishna Srinivas