Dmitro
Dmitro

Reputation: 1549

What is reason that Node.Js application uses 90% of CPU?

I made simple notification application that subscribes to redis channel and when it will have new message then this application send it to user.

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

And in log file sometimes in top hour I can find error message

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.

And node.js application's process uses 90% of CPU. Please advice how can I resolve this problem?

Upvotes: 0

Views: 234

Answers (1)

toxicate20
toxicate20

Reputation: 5410

Whenever your client connects to your server, you add another EventEmitter to the stack. Unfortunatly you are not closing them, so you are hitting the listeners limit of 11. You should track down the piece of code that adds the eventEmitters and then make another statement, that if there is already a client connected, there should not be any other emitters generated.

Upvotes: 1

Related Questions