Fix a few memory leaks; add /gc console command

3 memory leaks were fixed
  - ipThrottle (due to the periodic cleaner clearing the wrong object...)
  - ipCount (shouldn't have leaked very much, but removing obsolete data is good practice)
  - lastguestlogin (again, shouldn't leak much, but should be cleared periodically anyways)
A new console command (i.e. from the terminal running node) was added: /gc
  - If the process is invoked as node --expose-gc index.js, /gc allows you to manually invoke the garbage collector
This commit is contained in:
calzoneman 2014-04-10 21:54:46 -05:00
parent e973813718
commit 04dbb3444b
8 changed files with 53 additions and 16 deletions

View file

@ -12,7 +12,6 @@ var CONNECT_RATE = {
sustained: 0.1
};
// Keep track of rate limiting by IP
var ipThrottle = {};
// Keep track of number of connections per IP
var ipCount = {};
@ -80,6 +79,10 @@ function handleConnection(sock) {
sock.on("disconnect", function () {
ipCount[ip]--;
if (ipCount[ip] === 0) {
/* Clear out unnecessary counters to save memory */
delete ipCount[ip];
}
});
if (!(ip in ipCount)) {
@ -139,3 +142,21 @@ module.exports = {
}
}
};
/* Clean out old rate limiters */
setInterval(function () {
for (var ip in ipThrottle) {
if (ipThrottle[ip].lastTime < Date.now() - 60 * 1000) {
var obj = ipThrottle[ip];
/* Not strictly necessary, but seems to help the GC out a bit */
for (var key in obj) {
delete obj[key];
}
delete ipThrottle[ip];
}
}
if (Config.get("aggressive-gc") && global && global.gc) {
global.gc();
}
}, 5 * 60 * 1000);