NodeJS, tune or disable HTTP agent pooling

When using NodeJS's HTTP module to query you might end-up having performance problems if you do some heavy HTTP querying.

By default when executing queries using the HTTP module NodeJS will use connection pooling with the maximum number of connection sockets being 5, so if you hit the bottleneck your HTTP queries will lag as they will be queued.

To my knowledge you have 2 options to "counter" this behavior :

  1. Increase the number of http sockets
  2. Disable the HTTP agent per request

1. Increase the number of http sockets

    var http = require('http');

    // increase the maximum number of http connections in the pool
    http.globalAgent.maxSockets = 100;

    //regular http request options
    var options = {
        port: 1337,
        hostname: '127.0.0.1',
        method: 'GET',
        path: '/test'
  };

  // make http query
  var req = http.request(options, function(res) {
       console.log('STATUS: ' + res.statusCode);
       console.log('HEADERS: ' + JSON.stringify(res.headers));
       res.setEncoding('utf8');
       res.on('data', function (chunk) {
          console.log('BODY: ' + chunk);
      });
   });



2.Disable the HTTP agent per request


    var http = require('http');
  
      //regular http request options
      var options = {
         port: 1337,
         hostname: '127.0.0.1',
         method: 'GET',
         path: '/test',
         agent:false //disable the nodejs agent (no connection pooling)
      };

      // make http query
      var req = http.request(options, function(res) {
       console.log('STATUS: ' + res.statusCode);
       console.log('HEADERS: ' + JSON.stringify(res.headers));
       res.setEncoding('utf8');
       res.on('data', function (chunk) {
          console.log('BODY: ' + chunk);
      });
   });

Please be aware that changing this options might have side effects on your host system such as higher memory and bandwidth use so use it wisely

No comments:

Post a Comment

OSX show used ports or listening applications with their PID

On OSX you can display applications listening on a given port using the lsof the commands described below will show listening application...