When using the node http module, I get the following warning:
EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
In my code, I am not attaching multiple listner to the same event.
Here is an example program:
Test code
Client code
var options = {
hostname: "127.0.0.1",
method: 'POST',
port: 3000
};
var http = require("http");
http.globalAgent.maxSockets = 10;
var https = require("https");
var makeRequest = function (callback) {
var response;
var req = http.request(options, function (res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
response += chunk;
});
res.on('end', function () {
callback.apply(null, [null , response]);
return;
});
});
req.on('error', function (e) {
callback.apply(null, [e , response]);
return;
});
req.setSocketKeepAlive(true);
req.setTimeout(180000);
req.on('timeout', function () {
req.abort();
});
req.end();
}
var maxValue = 100;
var j = 1;
var i;
function doIt() {
for (i= 1; i <= maxValue; i++) {
makeRequest(function (error) {
if (error) {
console.log(arguments);
} else {
j++;
}
});
}
}
process.on('exit', function (){
console.log('Goodbye!');
console.log(i);
});
doIt();
Server code
var http = require('http');
var url = require("url");
var path = require("path");
http.globalAgent.maxSockets = 1000;
var https = require("https");
https.globalAgent.maxSockets = 1000;
var count = 0;
http.createServer(function (req, res) {
var uri = url.parse(req.url).pathname;
var body = "";
req.on('data', function (chunk) {
body += chunk;
});
req.on('end', function () {
setTimeout(function () {
sendResponse(res, "Hello");
}, 1);
});
}).listen(3000, '127.0.0.1');
function sendResponse(res, msg) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(msg);
};
What could be a mistake?
source
share