Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
498 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...