简体   繁体   English

Node.js:如何限制请求列表?

[英]Node.js: How to throttle a list of requests?

I'm writing a node.js app which needs to get some data from a list of pages from a provider: 我正在编写一个node.js应用程序,该应用程序需要从提供程序的页面列表中获取一些数据:

var list = [
  { url: 'http://www.example.com/1' },
  { url: 'http://www.example.com/2' },
  ...
  { url: 'http://www.example.com/N' },
];

Currently I'm using async.each , which works nicely: 目前,我正在使用async.each ,它很好地工作:

async.each(
  list, // 1st param is the array of items
  function(elem, callback) { // 2nd param is the function that each item is passed to
    request(elem.url, function (error, response, body) {
      if (!error && response.statusCode == 200) {
        console.log(body);
      }
    }),
  },
  function(err) { // 3rd param is the function to call when everything's done
    if (err) {
      console.error('Error in the final async callback:', err);
    }
  }
);

The only problem is that the site's server some times (understandably) responds with a 403 ( forbidden ) status code, due to an excess of requests from the same IP in the time unit... 唯一的问题是,由于时间单位中来自同一IP的请求过多,站点的服务器有时(可以理解)以403禁止 )状态代码响应...

I see async provides a whilst() method, too, whose example is: 我看到async提供了whilst()方法,其示例是:

var count = 0;
async.whilst(
  function () { return count < 5; },
  function (callback) {
    count++;
    setTimeout(callback, 1000);
  },
  function (err) {
    // 5 seconds have passed
  }
);

But I don't see how to use it to use it with a list, or how to use it combined with async.each ... :-( 但是我没有看到如何使用它与列表一起使用,或者如何将其与async.each ... :-(

So the answer is: How do I limit (throttle) a list of async requests in node.js? 因此答案是: 如何限制(限制)node.js中的异步请求列表?

PS: To be clearer, I don't want (if possible) to queue the requests, since a request could possibly take a long time to complete...: I just want the requests to be initiated at defined temporal intervals (say 5 ~ 10 seconds between each request...). PS:更明确地说,我不希望(如果可能)将请求排队 ,因为一个请求可能要花很长时间才能完成...:我只希望以定义的时间间隔启动请求(例如5每个请求之间要间隔10秒...)。


UPDATE: 更新:

After alireza david comment, I did try using async.eachLimit, which looked very promising, to me... This is an example of it's usage, on the module github site : 在alireza david评论之后,我确实尝试使用async.eachLimit,对我来说,这看起来非常有前途...这是其用法的一个示例,在模块github 站点上

async.eachLimit(
    obj.files,
    limit
    function (file, complete) {
      complete();
    },
    function (err) {
    }
);

But the limit usage is not documented, and it's not clear to me... If anybody has any clue... 但是没有记录使用限制,对我来说也不清楚...如果有人有任何线索...

Most of the time 403 means you should limit your requests, Because web server thinks you doing DDOS attack. 大多数时间403表示您应该限制请求,因为Web服务器认为您正在进行DDOS攻击。

In this situation you should async.eachLimit() 在这种情况下,您应该async.eachLimit()

async.eachLimit(obj.files, 1000,
    function (file, complete) {
      complete();
    },
    function (err) {

    });

UPDATE I think got it, The limit options is number of concurrence requests. 我想知道它的更新limit选项是并发请求的数量。 You should decrease this number (My opinion is 2 or 3 just for test) 您应该减少此数字(我的观点是2或3只用于测试)

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

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