There is a client that connects to a socket, communicates with the nodejs server, and disconnects. With each reconnect, the server starts to have more and more memory, eats up 1.5 Gb of RAM per month (under the conditions of the task it’s impossible for the daemon to fall, so restarting is not an option)
var _io = io.of('/signal') _io.on('connection', function (client) { var geoData, clientIp, onDisconnect = function () { client.removeListener('get_last', onGetLast); client.removeListener('login', onLogin); client.removeListener('logout', onLogout); client.removeListener('is_logged', onIsLogged); client.removeListener('disconnect', onDisconnect); delete onGetLast; delete onLogin; delete onLogout; delete onIsLogged; delete onDisconnect; delete geoData; delete clientIp; }, onGetLast = function () { config.DEBUG && console.log('on `get_last`'); if ('guests' === client.room) { client.emit('receive_signals', DATA.lastSignalsTime.slice(-10)); } else if ('logged' === client.room) { client.emit('receive_signals', DATA.lastSignals.slice(-10)); } }, onLogin = function (code) { config.DEBUG && console.log('on `login`', code); if (code === config.loginCode) { client.leave('guests'); client.join('logged'); client.room = 'logged'; //output = signals; } }, onLogout = function () { config.DEBUG && console.log('on `logout`'); client.leave('logged'); client.join('guests'); client.room = 'guests'; }, onIsLogged = function () { config.DEBUG && console.log('on `is_logged`'); var result; if ('logged' === client.room) { result = 1; } else { result = 0; // room 'guest' } client.emit('is_logged_res', result); }; clientIp = 'production' === config.NODE_ENV ? _.getClientIp(client) : '128.101.101.101'; geoData = DATA.mmdb.countries.getGeoDataSync(clientIp); client.room = 'guests'; client.join('guests'); client.on('disconnect', onDisconnect); client.on('get_last', onGetLast); //check if clients country is valid if (!geoData || !('object' === typeof geoData.country && null !== geoData.country && 'string' === typeof geoData.country.iso_code && -1 === DATA.invalidCountries.indexOf(geoData.country.iso_code)) ) { return; } client.on('login', onLogin); client.on('logout', onLogout); client.on('is_logged', onIsLogged); });