0

We're having a problem where every once in a while one of our environments our node app runs on 100% CPU. The server isn't very active and usually runs on 0%-2% CPU. I was wondering what are the common issues that might cause this problem and what would be the best way to find out what causing this issue.

below code on Server side code


var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var mongo = require('mongodb');
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://127.0.0.1:27017";
const fs = require('fs');
var port = process.env.PORT || 3110;

io.on('connection', function (socket) {
  console.log("connected");
  MongoClient.connect(url,{ useUnifiedTopology: true }, function (err, db) {
 
  socket.broadcast.on('getMessages', function (msg) {
      var dbo = db.db("Hitesh");
      if (err) throw err;
       console.log(msg)
      dbo.collection(msg["collectionName"]).find({ "roomId": msg["roomId"] }).sort({ $natural: -1 }).limit(parseInt(msg["limit"])).skip(parseInt(msg["total"])).toArray(function (err, result) {
        if (err) throw err;
        result.slice().reverse().forEach(element => {
          io.to(socket.id).emit(msg["roomId"], element);
        });
        //db.close;
      });
  });


  socket.broadcast.on('getMessages_once', function (msg) {
   // MongoClient.connect(url,{ useUnifiedTopology: true }, function (err, db) {
      var dbo = db.db("Hitesh");
      if (err) throw err;
      console.log(msg)
      dbo.collection(msg["collectionName"]).find({ "roomId": msg["roomId"] }).sort({ $natural: -1 }).limit(parseInt(msg["limit"])).skip(parseInt(msg["total"])).toArray(function (err, result) {
        if (err) throw err;
        console.log(result.length);
        io.to(socket.id).emit(msg["roomId"] + "_once", result.reverse());
        // result.slice().reverse().forEach(element => {

        // });
        //db.close;
      });
    //});
  });

  //---- send message to from client--///
  socket.broadcast.on('sendMessage', function (msg) {
    //MongoClient.connect(url,{ useUnifiedTopology: true }, function (err, db) {
      var dbo = db.db("Hitesh");
      if (err) throw err;
      dbo.collection(msg["collectionName"]).insertOne(msg["data"], function (err, res) {
        if (err) throw err;
        var messsageData = {
          "roomId": msg["roomId"],
          "messageId": msg["messageId"],
          "collectionName": "conversations"
        };
        getMessageCount(messsageData);
        console.log("1 document inserted");
        msg["limit"] = "1";
        msg["total"] = "0";

        var val = { $set: msg["lastMessage"] };
        var Ids = { "roomId": msg["roomId"] };
        dbo.collection("conversations").findOneAndUpdate(Ids, val, { upsert: true }, function (err, res) {
          if (err) throw err;

          var FullArray = res["value"]["userList"];

          FullArray.forEach(element => {
            var someString = element;
            someString["collectionName"] = "conversations";
            MemberList(someString);
          });
        });

        dbo.collection(msg["collectionName"]).find({ "roomId": msg["roomId"] }, { limit: 1 }).sort({ $natural: -1 }).toArray(function (err, result) {
          if (err) throw err;
          result.forEach(element => {
            io.emit(msg["roomId"], element);
          });
          //db.close;
        });
      });
    //});
  });



  //-- add user to database -----///
  socket.broadcast.on('createUser', function (msg) {
    console.log("createUser");
    //MongoClient.connect(url,{ useUnifiedTopology: true }, function (err, db) {
      var dbo = db.db("Hitesh");
      dbo.collection(msg["collectionName"]).find({ "userId": msg["userId"], "title": msg["title"] }).toArray(function (err, result) {
         console.log(result);
        if (result.length == 0) {
          dbo.collection(msg["collectionName"]).insertOne(msg, function (err, converRes) {
            console.log(converRes);
            //db.close;
          });
        }
        else {
          //db.close;
        }
      });
    //});
  });



  // --------   create Room   ----///
  socket.broadcast.on('createRoom', function (msg) {
    //console.log("createRoom");
   // MongoClient.connect(url,{ useUnifiedTopology: true }, function (err, db) {
      var dbo = db.db("Hitesh");
      var dataObject = msg["data"];
      var roomid = dataObject["roomId"];
      dbo.collection(msg["collectionName"]).find({ "roomId": roomid }).toArray(function (err, result) {
        if (result.length == 0) {
          dbo.collection(msg["collectionName"]).insertOne(msg["data"], function (err, converRes) {
            // console.log(converRes["insertedId"]);
            io.emit('roomId', converRes["insertedId"]);
            //db.close;
          });
        }
        else {
          //db.close;
        }
      });
   // });
  });


  //---- get member list --////
  socket.on('getChatUserList', function (msg) {
    MemberList(msg);
  });

  function MemberList(msg) {
    //MongoClient.connect(url,{ useUnifiedTopology: true }, function (err, db) {
      var dbo = db.db("Hitesh");
      if (err) throw err;
      var nameTopost = msg["userId"] + "_" + msg["title"];

      dbo.collection(msg["collectionName"]).find({ "userList.userId": msg["userId"], "userList.title": msg["title"] }).limit(parseInt(msg["limit"])).skip(parseInt(msg["total"])).toArray(function (err, result) {
        if (err) throw err;

        var userIdlist = [];
        result.forEach(element => {
          var someObject = element["userList"];

          someObject.forEach(obj => {
            userIdlist.push(obj)
          });
        });


        dbo.collection("users").find({ $or: userIdlist }).toArray(function (err, userData) {
          //console.log(userData);
          var someData = {
            "RoomList": result,
            "userData": userData,
          };
          //console.log(someData);
          io.emit(nameTopost, someData);
          //db.close;
        });

      });

   // });
  }


  ///----- get total message count ----/////
  socket.on('getTotalCount', function (msg) {
    getTotalCount(msg);
  });


  ///----- set total message count ----/////
  socket.on('setTotalCount', function (msg) {
    //MongoClient.connect(url,{ useUnifiedTopology: true }, function (err, db) {
      var dbo = db.db("Hitesh");
      var val = { $set: msg };
      var Ids = { "totalCountId": msg["totalCountId"] };
      dbo.collection(msg["collectionName"]).findOneAndUpdate(Ids, val, { upsert: true }, function (err, res) {
        getTotalCount(msg);
      });
    //});
  });



  function getTotalCount(msg) {
    //MongoClient.connect(url,{ useUnifiedTopology: true }, function (err, db) {
      var dbo = db.db("Hitesh");
      dbo.collection(msg["collectionName"]).find({ "totalCountId": msg["totalCountId"] }).toArray(function (err, result) {
        io.emit(msg["totalCountId"], result[0]);
        //db.close;
      });
    //});
  }



  socket.on('getMessageCount', function (msg) {
    getMessageCount(msg);
  });


  function getMessageCount(msg) {
   // MongoClient.connect(url,{ useUnifiedTopology: true }, function (err, db) {
      if (err) throw err;
      var dbo = db.db("Hitesh");
      dbo.collection(msg["collectionName"]).find({ "roomId": msg["roomId"] }).toArray(function (err, result) {
        io.emit(msg["messageId"], result[0]);
        //db.close;
      });
   // });
  }

  socket.on('setMessageCount', function (msg) {
   // MongoClient.connect(url,{ useUnifiedTopology: true }, function (err, db) {
      if (err) throw err;
      var dbo = db.db("Hitesh");
      var val = { $set: msg["data"] };
      var Ids = { "roomId": msg["roomId"] };
      dbo.collection(msg["collectionName"]).findOneAndUpdate(Ids, val, { upsert: true }, function (err, res) {
        //db.close
        getMessageCount(msg);
      });
   // });
  });

  });
});




http.listen(port, function () {
  console.log('listening on *:' + port);
});


.... ..... ... ... ... I am not sure whats causing this issue

Following is the CPU usage

3
  • How are the clients you are making for every connection closed? Commented Aug 18, 2020 at 19:08
  • inside andriod and ios application when the app enter background then closes the connection @D.SM Commented Aug 18, 2020 at 19:12
  • So you are not closing the clients? Explains the resource leak. Commented Aug 18, 2020 at 20:05

1 Answer 1

3

Seems like you are creating a new mongo connection on every new socket connection:

io.on('connection', function (socket) {
  console.log("connected");
  MongoClient.connect(url,{ useUnifiedTopology: true }, function (err, db) {

You should rather create a single connection and share that. I would normally create a connection to the db and then start the HTTP and socket server.

Snippet using mongoose but concept is the same - open the connection, then setup server and handlers:

mongoose.connect(`${config.db.uri}/${config.db.database}`, {useNewUrlParser: true, useUnifiedTopology: true, autoIndex: false});
const db = mongoose.connection;

db.once('open', () => {
    const app = express();

    app.db = db;
    ...

    const http = require('http').createServer(app);
    const io = require('socket.io')(http);
    ...

    http.listen(config.server.port, () => {
       console.log(`listening on ${config.server.port}`);
    });

    ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.