求助:emitter.setMaxListeners()
(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace
at Socket.EventEmitter.addListener (events.js:168:15)
at Socket.EventEmitter.once (events.js:189:8)
at Socket.setTimeout (net.js:173:12)
at ClientRequest.setTimeout (http.js:1557:17)
at ClientRequest.setTimeout (http.js:1569:10)
at ClientRequest.g (events.js:185:14)
at ClientRequest.EventEmitter.emit (events.js:115:20)
at ClientRequest.onSocket (http.js:1514:9)
at process.startup.processNextTick.process._tickCallback (node.js:244:9)
使用request模块发送请求,当请求多的时候就出现这个警告,谁能告诉我一下应该在哪儿设置setMaxListeners(),我在自己的环境中没有调试出来。
2 回复
治标:
var req=http.request(options,function(res){
//......
});
/** 此处为req.once,而非req.on,二者区别见官方文档api说明**/
req.once('socket',function(socket){
/** 默认listener数是10,提高到20应该足够**/
socket.setMaxListeners(20);
});
要治本,将timeout的回调函数独立出来,在response事件之后手动注销timeout的监听
var req=http.request(options,function(res){
req.removeAllListeners();//clear out all listeners
req.setTimeout(0,timeout_callback);//clear out timeout listener
//do more work.......
});
function timeout_callback(){
//abort request?......
}
/** for example, timeout in 30s**/
req.setTimeout(30000,timeout_callback);
PS: 记得node某个版本后clientrequest会自动注销监听,楼主的版本很旧?
非常感谢