Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

node.js - Nodejs HTTP server handling multiple requests inserting into DB

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...