Nodejs HTTP server handling multiple requests inse

2019-09-06 01:59发布

问题:

I have an http server and every time it gets a post request, it is supposed to insert the data into MongoDB. This server is supposed to be constantly running and accepting thousands of request in any given second.

How can I maximize the efficiency and speed of my code? I feel like my current code is not fast enough and furthermore wastes CPU power when it makes a new db every time it receives a request.

My current layout

var server = http.createServer(function (req, res) {

     req.on('data', function(chunk) {
          //Receive my data
     });

     req.on('end', function() {
          //JSON parse my data
          var db = new Db('test', new Server("111.111.11.111", 27017,{auto_reconnect: false}), {safe: true});  

          db.open(function(err, db) {
               //Insert data into DB
               db.close();
          });
     });
}); //End Http server
server.listen(8999);

I have tried replacing db.open with MongoClient.connect, but that considerably slows down processing and I don't know why. In this case, the older version of MongoDB Native for node js seems to work faster.

回答1:

You'll want to shift to an approach where you open a large pool of connections during startup that are shared by your HTTP request handlers. To tweak the MongoDB connection pool size to suit whatever scalability needs you have, pass an options parameter to your MongoClient.connect call.

var options = {
    server: {
        // The number of pooled connection available to your app.
        poolSize: 100
    }
};

mongodb.MongoClient.connect('mongodb://111.111.11.111/test', options, 
    function(err, db) {
        var server = http.createServer(function (req, res) {
            // Your req.on calls go here, directly using db rather than creating
            // new instances. Don't close db either.
        });
        server.listen(8999);  
    }
);


回答2:

Not sure if this would be better, but you can encapsulate that server inside the db, therefore persisting the connection:

var db = new Db('test', new Server("111.111.11.111", 27017,{auto_reconnect: false}), {safe: true});  

db.open(function(err, db) {
    var server = http.createServer(function (req, res) {
        //now do stuff thru constant open connection
    });      
    db.close();
});