【问题标题】:Node.js How to setMaxListeners()Node.js 如何设置MaxListeners()
【发布时间】:2016-09-17 19:20:49
【问题描述】:

我以前从未使用过 node.js。但是按照here 的步骤进行 solr 安全代理。一切正常,但它给了我这个警告。我知道我必须设置最大监听器,但不知道我如何以及在哪里做。请帮帮我。

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at addListener (events.js:160:15)
    at /home/abc/node_modules/solr-security-proxy/node_modules/http-proxy/lib/node-http-proxy/routing-proxy.js:63:27
    at Array.forEach (native)
    at null.<anonymous> (/home/abc/node_modules/solr-security-proxy/node_modules/http-proxy/lib/node-http-proxy/routing-proxy.js:62:33)
    at emit (events.js:98:17)
    at addListener (events.js:138:10)
    at Array.0 (/home/abc/node_modules/solr-security-proxy/solr-security-proxy.js:69:13)
    at Server.handler (/home/abc/node_modules/solr-security-proxy/node_modules/http-proxy/lib/node-http-proxy.js:175:40)
    at Server.emit (events.js:98:17)
    at HTTPParser.parser.onIncoming (http.js:2164:12)
(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
    at addListener (events.js:160:15)
    at Array.0 (/home/abc/node_modules/solr-security-proxy/solr-security-proxy.js:69:13)
    at Server.handler (/home/abc/node_modules/solr-security-proxy/node_modules/http-proxy/lib/node-http-proxy.js:175:40)
    at Server.emit (events.js:98:17)
    at HTTPParser.parser.onIncoming (http.js:2164:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:152:23)
    at Socket.socket.ondata (http.js:2022:22)
    at TCP.onread (net.js:528:27)

【问题讨论】:

    标签: node.js


    【解决方案1】:

    我遇到这个问题是因为我试图运行超过 10 个异步任务,所以我确实需要超过 10 个事件监听器。但是实际的事件监听器代码在我正在使用的库/模块中,所以我无法使用setMaxListeners(),因为我无权访问该对象。最后,在程序开始时覆盖全局值对我有用:

    require('events').EventEmitter.defaultMaxListeners = 15;
    

    【讨论】:

      【解决方案2】:

      您在调用addListener(或on)的同一对象上调用setMaxListeners,这将是一个EventEmitter

      但是,如果您确实存在发射器内存泄漏(而不是碰巧需要 11 个侦听器而不再需要),那么您需要从源头解决该问题 - 提高限制对您没有帮助。

      见:How to debug “EventEmitter memory leak detected. 11 listeners added”

      【讨论】:

        猜你喜欢
        • 2014-03-09
        • 2021-05-22
        • 2017-02-28
        • 2018-01-25
        • 2013-11-09
        • 1970-01-01
        • 2015-03-04
        • 2011-11-10
        • 1970-01-01
        相关资源
        最近更新 更多