简体   繁体   中英

Http Request can't send a response immediately in Nodejs server

I send JSON requests one by one to the nodejs server. After 6th request, server can't reply to the client immediately and then it takes a little while(15 seconds or little bit more and send back to me answer 200 ok) It occurs a writing json value into MongoDB and time is important option for me in terms with REST call. How can I find the error in this case? (which tool or script code can help me?) My server side code is like that

var controlPathDatabaseSave = "/save";
app.use('/', function(req, res) {
console.log("req body app use", req.body);
var str= req.path;

if(str.localeCompare(controlPathDatabaseSave) == 0)
{
    console.log("controlPathDatabaseSave");
    mongoDbHandleSave(req.body);
    res.setHeader('Content-Type', 'application/json');
    res.write('Message taken: \n');
    res.write('Everything all right with database saving');
    res.send("OK");
    console.log("response body", res.body);
}

});

My client side code as below:

function saveDatabaseData()
{
    console.log("saveDatabaseData");
        var oReq = new XMLHttpRequest();
        oReq.open("POST", "http://192.168.80.143:2800/save", true);
        oReq.setRequestHeader("Content-type", "application/json;charset=UTF-8");
        oReq.onreadystatechange = function() {//Call a function when the state changes.
            if(oReq.readyState == 4 && oReq.status == 200) {
                console.log("http responseText", oReq.responseText);
            }
        }
        oReq.send(JSON.stringify({links: links, nodes: nodes}));
}

--Mongodb save code

function mongoDbHandleSave(reqParam){
//Connect to the db
    MongoClient.connect(MongoDBURL, function(err, db)
    {
        if(!err)
        {
            console.log("We are connected in accordance with saving");
        } else
        {
            return console.dir(err);
        }

    /*
        db.createCollection('user', {strict:true},  function(err, collection) {
            if(err)
                return console.dir(err);
        });
    */
        var collection = db.collection('user');
        //when saving into database only use req.body. Skip JSON.stringify() function
        var doc = reqParam;
        collection.update(doc, doc, {upsert:true});
    });

}

You can see my REST call in google chrome developer editor. (First six call has 200 ok. Last one is in pending state)

在此处输入图片说明

--Client output

在此处输入图片说明

--Server output

在此处输入图片说明

Thanks in advance,

Since it looks like these are Ajax requests from a browser, each browser has a limit on the number of simultaneous connections it will allow to the same host. Browsers have varied that setting over time, but it is likely in the 4-6 range. So, if you are trying to run 6 simultaneous ajax calls to the same host, then you may be running into that limit. What the browser does is hold off on sending the latest ones until the first ones finish (thus avoiding sending too many at once).

The general idea here is to protect servers from getting beat up too much by one single client and thus allow the load to be shared across many clients more fairly. Of course, if your server has nothing else to do, it doesn't really need protecting from a few more connections, but this isn't an interactive system, it's just hard-wired to a limit.

If there are any other requests in process (loading images or scripts or CSS stylesheets) to the same origin, those will count to the limit too.

If you run this in Chrome and you open the network tab of the debugger, you could actually see on the timline exactly when a given request was sent and when its response was received. This should show you immediately whether the later requests are being held up at the browser or at the server.

Here's an article on the topic: Maximum concurrent connections to the same domain for browsers .

Also, keep in mind that, depending upon what your requests do on the server and how the server is structured, there may be a maximum number of server requests that can efficiently processed at once. For example, if you had a blocking, threaded server that was configured with one thread for each of four CPUs, then once the server has four requests going at once, it may have to queue the fifth request until the first one is done causing it to be delayed more than the others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM