简体   繁体   English

Node.js请求循环限制

[英]Nodejs request loop limit

This is my first nodejs application. 这是我的第一个nodejs应用程序。 I am trying to do a cronjob with fetching some external API and do some calculation of the response. 我正在尝试通过获取一些外部API来进行cronjob并进行响应的一些计算。

Everything works fine until the curlList is to big. 一切正常,直到curlList变大为止。 With 10 items in curlList it's okey, but I have a big big curlList more than 90 items. 在curlList中有10个项目是不错的,但是我有一个很大的curlList超过90个项目。

What is the best way to do this implementation. 什么是执行此实现的最佳方法。

Thanks for helping. 感谢您的帮助。

Best regards, Johnny 最好的问候,约翰尼

var request = require('request');

var curlList = [{
    id: 1,
    href: '/name1'
}, {
    id: 2,
    href: '/name2'
}, {
    id: 3,
    href: '/name3'
}];

var curl = function(id, url) {
    var payload = {
        id: id
    };

    var options = {
        method: 'post',
        body: payload,
        json: true,
        url: ""
    }

    request(options, function(err, res, body) {
        if (err) {
            console.log(err, 'error posting json')
            return
        }
        //Calculate response data

        //If match 
        if (match) {
            console.log(url);
        }
    });
};

app.listen(3000, function() {

    for (var i = 0; i < curlList.length; i++) {
        var href = list[i].href;
        var id = list[i].id;
        curl(id, href);
    }

});

Your for loop starts all the defined requests in a matter of few microseconds - websites usually detect such obtrusive behaviour as an attempt to overload the server (a DoS attack). 您的for循环会在几微秒内启动所有定义的请求-网站通常会检测到诸如试图使服务器超载( DoS攻击)之类的干扰性行为。 It is also not a good idea to do this due to your own hardware / network limitations - if you needed to issue 1000 requests and each response would have 1 MB, you suddenly need to download 1 GB of response data. 由于您自己的硬件/网络限制,这样做也不是一个好主意-如果您需要发出1000个请求,而每个响应将有1 MB,则突然需要下载1 GB的响应数据。 What's worse, your network might get so overloaded that some requests will simply time out. 更糟糕的是,您的网络可能会过载,以至于某些请求只会超时。

You need to add some kind of throttling to limit the amount of requests being made at any given time to some reasonable amount. 您需要添加某种限制,以将在任何给定时间进行的请求数量限制为合理的数量。 I personally recommend the async.js library, particuarly its eachLimit() utility. 我个人推荐async.js库,尤其是它的eachLimit()实用程序。

Modify your code to do something like this; 修改您的代码以执行以下操作;

var request = require('request');

var curlList = [{
    id: 1,
    href: '/name1'
}, {
    id: 2,
    href: '/name2'
}, {
    id: 3,
    href: '/name3'
}];

var curl = function(id, url, done) {
    var payload = {
        id: id
    };

    var options = {
        method: 'post',
        body: payload,
        json: true,
        url: ""
    }

    request(options, function(err, res, body) {
        done(err);

        if (err) {
            console.log(err, 'error posting json')
            return
        }
        //Calculate response data

        //If match
        if (match) {
            console.log(url);
        }
    });
};

app.listen(3000, function() {
    int current = 1;
    int max = 5; // max 5 parallel
    var scheduleJobs = function() {
        current--;
        while(current < max) {
            current++;
            var job = curList.shift();
            curl(job.id, job.href, scheduleJobs);
        }
    }
    scheduleJobs();
});

This allows max 5 parallel requests. 这最多允许5个并行请求。

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

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