2011-12-31 4 views
3

아래의 소켓 서버는 3-4 일 동안 정상적으로 작동하지만 Munin이 지정한 CPU 범위 (160)를 벗어났다는 알림을받은 후 기억이 내게 들리는 것처럼 들리는데, 정확하게는 알지만 찾을 수는 없다.높은 CPU를 사용하는 Socket.io 노드 서버

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 
     } 
    }); 
}); 

답변

2

나는 문제가 당신이 setIntervalget_notification 이벤트가 발생할 때마다 불 것으로 생각합니다. 이러한 간격은 시작한 후에는 작동을 멈추지 않습니다.

특정 조건이 충족 될 때 해당 간격을 중지시키는 코드를 작성해야합니다 (시간 초과? 클라이언트 분리?). 또는 setIntervalsetTimeout으로 변경하십시오. 예를 들어 연결이 끊어진 클라이언트에 데이터를 보낼 수없는 경우와 같이 예외가 발생하지 않는 한 자체적으로 실행됩니다.

+0

아, 나는 '시간 초과'로 변경해보고 며칠 후에 다시 돌아올 것입니다. 감사합니다 :) – Christoffer

+0

이것은 내 문제를 해결 :) – Christoffer

관련 문제