尽管使用了最新版本的nodejs,仍会发生“EventEmitter内存泄漏”

时间:2013-09-17 12:53:06

标签: javascript node.js

经过艰苦的尝试和谷歌搜索,我仍然无法弄清楚如何解决这个问题。 需要帮助PLZ ......

输出:

    (node) warning: possible EventEmitter memory leak detected. 51 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at EventEmitter.addListener (events.js:160:15)
    at EventEmitter.once (events.js:179:8)
    at SkinCollection.open (/home/nitin/node_modules/mongoskin/lib/mongoskin/collection.js:108:20)
    at obj.(anonymous function) [as insert] (/home/nitin/node_modules/mongoskin/lib/mongoskin/utils.js:64:10)
    at insertErrorLog (/home/nitin/dev/aayush/errorLog.js:37:18)
    at Server.<anonymous> (/home/nitin/dev/aayush/errorLog.js:69:24)
    at Server.EventEmitter.emit (events.js:98:17)
    at HTTPParser.parser.onIncoming (http.js:2022:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:119:23)
    at Socket.socket.ondata (http.js:1912:22)

代码链接:link to code

Thnax提前寻求帮助......

1 个答案:

答案 0 :(得分:0)

看起来db.collection只要你调用它就会创建一个EventEmitter。您是否应该尝试在请求处理程序之外缓存集合:

var logs = db.collection('logs');

var insertErrorLog = function (req, res) {
  var pathname = url.parse(req.url).pathname;
  var ua = req.headers['user-agent'];     // user-agent header from an HTTP request
  var details = parser.setUA(ua).getResult();
  var params = url.parse(req.url,true).query;

  logs.insert({…});

快速查看代码显示它应该始终返回相同的实例,但我看不出任何其他原因导致您的问题:

https://github.com/kissjs/node-mongoskin/blob/master/lib/mongoskin/db.js#L148