当从错误块调用回调时,套接字描述符继续增加

时间:2018-02-05 06:37:00

标签: node.js sockets websocket callback file-descriptor

我正在执行以下代码

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));
    } 
  });
};

当从瀑布模型调用callFirstEvent函数时,如果它停留在callingFirstEvent的错误块中

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

套接字描述符增加。

以下是我如何检查套接字描述符的总数

ls -ltr / proc / cat /path of pid / fd / | grep socket | wc -l <​​/ p>

如何在打开套接字描述符后关闭套接字描述符?或者有没有办法不打开这些插座?

1 个答案:

答案 0 :(得分:0)

您正在泄漏套接字。大多数错误,实际上除了读取超时之外的所有错误都对套接字是致命的,并且应该导致您关闭套接字。