Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

node.js - Socket descriptors keep on increasing when callback is called from error block

I am executing following code

var pg = require('pg').native;

async.waterfall([
      function (callback) {
          //some code
      },
      function (result,callback) {
          logger.debug('Async waterfall 2: Querying events.');
          try {
              callingFirstEvent(queryMode, request, foundEvents, callback);
          } catch(ex) {
              logger.error('callingFirstEvent::Error occurred in async waterfall 2: ' + ex.message);
          }
      },
      function (result, callback) {
          //some code
      },
      ], 
      function (err, res) {
      //some code
  }
  );


var callingFirstEvent = function (queryMode, request, foundEvents, callback) {
    var conStringList = queryMode.conStringList;
    var executeQuery = function (conString, limit, next) {
    pg.connect(conString, function(err, client, done) {
      //below error code is reason of increased socket descriptors
      if (err) {  
        logger.error('callingFirstEvent : Cannot fetch a client from pool', err);
        next(null, limit); 
        return;
      }

      client.query(sql, function (err, result) {
        done();
        if (err) {
          logger.error('callingFirstEvent', err);
          logger.error('sql:' + sql);
          next(err, limit);
          return;
        }
        //some code
      }); // client.query
    }); // pg.connect
  }; // executeQuery

  var schedule = [];
  conStringList.forEach(function (conString, index) {
    if (index == 0) {
      schedule.push(async.apply(executeQuery, conString, request.query.max));
    } 
  });
};

When callingFirstEvent function is called from waterfall model and if it stuck in error block of callingFirstEvent

 if (err) {
        logger.error('callingFirstEvent : Cannot fetch a client from pool', err);
        next(null, limit);
        return;
      }

socket descriptors increased.

Here is how I am checking for total number of socket descriptors

ls -ltr /proc/cat /path of pid/fd/ | grep socket | wc -l

How to close socket descriptors after they are opened? or is there any way not to open these sockets?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are leaking sockets. Most errors, in fact all errors except read timeouts, are fatal to the socket and should cause you to close the socket.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...