简体   繁体   English

节点服务器中的http请求缓慢

[英]Slow http request in node server

In my application, I have to create an API for searching n items. 在我的应用程序中,我必须创建一个用于搜索n个项目的API。 The server logic is to find for items in it's own database and if the result count is less than n , then use search results from a third party service to fill in the remaining results. 服务器逻辑用于在其自己的数据库中查找项目,如果结果计数小于n ,则使用来自第三方服务的搜索结果来填充剩余的结果。

The problem I'm facing over here is, the time taken to execute the third party request. 我在这里遇到的问题是,执行第三方请求所花费的时间。
On an average it takes around 900-1500 ms to execute the third party request from the server, however if I execute the same request from browser or any other rest clients, the same takes around 300-400 ms . 从服务器执行第三方请求平均需要大约900-1500毫秒 ,但是如果我从浏览器或任何其他其他客户端执行相同的请求,则大约需要300-400毫秒

This is what I'm doing 这就是我正在做的事情

router.get('/search/:searchParam', function (req, res, next) {
    async.parallel({
        local : function(callback){
            //Search for object in DB
            callback(null,localResults);
        },
        fallback : function(callback) {
            var url = <searchURL>+<queryParam>;
            //This takes 900-1500 ms
            request(url, function (error, response, body) {
                if(error){
                    return callback(err);
                }
                callback(null,JSON.parse(body));
            });
        }
    }, function(err, results){
        if(err){
            return reject(err);
        }
        if(results.local.length < searchLimit) {
            results.local.push.apply(results.local,_.first(results.fallback,searchLimit-results.local.length));
            results.local = results.local.reduce(function(a,b){if(a.indexOf(b)<0)a.push(b);return a;},[]);
        }
        res.json(results.local);
    })
})

The avg time taken is : 平均时间是:
A. 40-100 ms for local search A.本地搜索40-100毫秒
B. 900-1500 ms for fallback search B.回退搜索的900-1500毫秒
A+B. A + B。 1000-1500ms Total time 1000-1500ms总时间

I'm not getting how to improve the overall response time. 我没有得到如何改善整体响应时间。
Any suggestions? 有什么建议?

Its probably because you aren't using keepalive with your requests. 这可能是因为您没有在请求中使用keepalive。 I think this will probably fix it, but I could be wrong. 我想这可能会解决它,但我可能是错的。

request({url: url, forever: true}, function (error, response, body) {

不要使用async库,而是尝试在本地数据库搜索的请求回调中调用第三方API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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