My socket server below works fine for 3-4 days, but after dat I get notifications from Munin that it’s outside specified CPU range (160). Sounds to me like memory isn’t free:d correctly, but I can’t find where?
var io = require('socket.io').listen(80);
var Memcached = require('memcached');
var md5 = require('MD5');
var memcached = new Memcached(['IP']);
var interval_time = 5000;
loglevel = process.argv[2];
if(loglevel == "") loglevel = 1;
io.enable('browser client minification'); // send minified client
io.enable('browser client etag'); // apply etag caching logic based on version number
io.enable('browser client gzip'); // gzip the file
io.sockets.on('connection', function (socket)
{
socket.on('get_notifications', function (data)
{
// verify authorization
if(data.key == 'XXXXXXX')
{
socket.user_id = data.user_id;
setInterval(function()
{
memcached.get(["ntf_" + socket.user_id, "nnt_" + socket.user_id], function(error, mem_result)
{
num_orders = mem_result['ntf_' + socket.user_id];
is_searching = mem_result['nnt_' + socket.user_id];
if(typeof(socket.prev_notification)==='undefined') socket.prev_notification = {};
var notification = socket.prev_notification;
var have_new_notifications = false;
if(is_searching != socket.prev_notification.nnt)
{
have_new_notifications = true;
notification.nnt = is_searching;
}
if(num_orders != socket.prev_notification.ntf)
{
have_new_notifications = true;
notification.ntf = num_orders;
}
if(have_new_notifications)
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
console.log(h+":"+m+":"+s+' - sending notification to ' + data.user_id);
console.log(notification);
socket.emit('notifications', notification);
socket.prev_notification = notification;
}
});
}, interval_time); // interval
}
});
});
I think the problem is that you fire
setIntervalevery timeget_notificationevent is fired. These intervals never stop working once they started.You should write some code which will stop those intervals when certain conditions are met (timeout? client disconnect? whatever). Or change
setIntervaltosetTimeoutwhich will fire itself unless exception is thrown (like for example cannot send data to disconnected client).