从错误块调用回调时,套接字描述符不断增加 [英] Socket descriptors keep on increasing when callback is called from error block

查看:61
本文介绍了从错误块调用回调时,套接字描述符不断增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行以下代码

  var pg = require(’pg’)。native; 

async.waterfall([
函数(回调){
//一些代码
},
函数(结果,回调){
logger.debug(异步瀑布2:查询事件。);
试试{
callFirstEvent(queryMode,request,foundEvents,callback);
} catch(ex){
logger.error('callingFirstEvent :: Error发生在异步瀑布2:'+ ex.message);
}
},
函数(结果,回调){
/ / some code
},
],
function(err,res){
//一些代码
}
);


varcallingFirstEvent =函数(queryMode,request,foundEvents,callback){
var conStringList = queryMode.conStringList;
var executeQuery = function(conString,limit,next){
pg.connect(conString,function(err,client,done){
//下面的错误代码是套接字描述符增加的原因
if(err){
logger.error('callingFirstEvent:无法从池中获取客户端',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;
}
//一些代码
}); // client.query
}); // pg.connect
}; // executeQuery

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

从瀑布模型调用callFirstEvent函数时,如果卡在调用FirstEvent的错误块中,则

  if(err){
logger.error('callingFirstEvent:无法从池中获取客户端',err);
next(null,limit);
的回报;
}

套接字描述符增加。



这是我检查套接字描述符总数的方式



ls -ltr / proc / cat / pid的路径 / fd / | grep套接字| wc -l <​​/ p>

打开套接字描述符后如何关闭它们?还是有什么办法不打开这些套接字?

解决方案

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


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?

解决方案

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.

这篇关于从错误块调用回调时,套接字描述符不断增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆