2012-11-19 76 views
0

我做了訂閱Redis的頻道以及何時將有新的消息,那麼這個應用程序將其發送給用戶簡單的通知應用程序。Node.Js應用程序使用90%CPU的原因是什麼?

client.on("auth", function(sessId, userId){ 
     console.log('AUTH: userId ' + userId) 
     var userIdFromRedis; 
     redis1.get('PHPREDIS_SESSION:' + sessId, function (err , reply) { 

      if (reply){ 
       reply = reply.toString(); 
       var result = reply.match(/s:2:\"id\";i:(\d+);/); 

       if (result) { 
        userIdFromRedis = result[1] 
        console.log('AUTH: userIdFromRedis ' + userIdFromRedis) 
       } else { 
        result = reply.match(/s:7:\"guestId\";i:(\d+);/); 
        if (result) { 
         var guestIdFromRedis = result[1] 
         console.log('AUTH: guestIdFromRedis ' + guestIdFromRedis) 
        } 
       } 

       if (userIdFromRedis == userId) { 
        client.userId = userId; 
        subscribe.subscribe(channelPrefix + userId); 
        clients[userId] = client; 
        client.emit("auth", {"success":true}); 
        console.log('AUTH: result - ok') 
       } else if (guestIdFromRedis) { 
        client.guestId = guestIdFromRedis; 
        subscribe.subscribe(channelPrefix + guestIdFromRedis); 
        clients[guestIdFromRedis] = client; 
        client.emit("auth", {"success":true}); 
        console.log('AUTH: result - ok') 
       } else { 
       client.disconnect(); 
       console.log('AUTH: result - fail') 
       } 
      } else { 
       client.disconnect(); 
      } 
     }); 
    }) 

    subscribe.on("message", function(channel, message) { 
     var userId = Math.round(channel.substr(channelPrefix.length)); 
     if (client.userId == userId || client.guestId == userId) { 
      console.log('Subscriber: ' + message) 
      client.send(message); 
     } 
    }); 

    client.on("message", function(text){ 
      client.send(text); 
    }) 

並在日誌文件中有時頂小時我可以找到錯誤消息

(節點)警告:可能EventEmitter內存泄漏檢測。已添加11 聽衆。使用emitter.setMaxListeners()來增加限制。

和Node.js的應用程序的過程中使用90%的CPU。 請諮詢我該如何解決這個問題?

+0

我的猜測是你正在訂閱「消息」 – toxicate20

+0

我需要訂閱什麼? – Dmitro

回答

1

每當你的客戶端連接到你的服務器,你再添EventEmitter堆棧。不幸的是,你沒有關閉它們,所以你正在達到11個聽衆限制。你應該追蹤添加eventEmitters的代碼片段,然後做出另一個聲明,如果已經連接了一個客戶端,則不應該有其他的生成發射器。

+0

我可以添加io.setMaxListeners(0),但這是解決問題的真正方法嗎? – Dmitro

+0

不,這可能不是解決方案。我有與binaryJS相同的問題,無法弄清楚。 – toxicate20

相關問題