简体   繁体   English

在Node.js中将数千个并发的https请求划分为较小的块

[英]Dividing thousands of concurrent https requests into smaller blocks in Nodejs

I am working on a project where thousands of concurrent https calls are made. 我正在一个项目中进行数千个并发的https调用。 My first question is: Where is the concurrence limit? 我的第一个问题是: 并发限制在哪里? How many calls can be made concurrently? 可以同时拨打多少个电话? On what does it depends? 取决于什么? Secondly, I work on ubuntu system and in most of the cases, the system hangs because of too many requests. 其次,我在ubuntu系统上工作,在大多数情况下,由于请求过多而导致系统挂起。 How can i divide these thousands of concurrent requests into blocks of something like 50 or 100 requests at a time and on completion of those, the next set of requests are called. 我如何一次将这数千个并发请求分成大约50或100个请求的块,并在这些请求完成后调用下一组请求。

my code looks like: 我的代码如下:

items.forEach(function(item) {
        request().then(function() {
            if(counter == items.length) {
                deferred.resolve("Success");
                return deferred.promise;
            }
        })
        .catch(function(err) {
            deferred.reject(err);
        });
        counter++;
    });

Items array is mostly more than 10k. Items数组通常超过10k。

You can use Promise.map concurrency options in bluebird 您可以在bluebird中使用Promise.map并发选项

var Promise = require("bluebird");
var join = Promise.join;
var fs = Promise.promisifyAll(require("fs"));
var concurrency = parseFloat(process.argv[2] || "Infinity");
console.time("reading files");
fs.readdirAsync(".").map(function(fileName) {
    var stat = fs.statAsync(fileName);
    var contents = fs.readFileAsync(fileName).catch(function ignore() {});
    return join(stat, contents, function(stat, contents) {
        return {
            stat: stat,
            fileName: fileName,
            contents: contents
        }
    });
// The return value of .map is a promise that is fulfilled with an array of the mapped values
// That means we only get here after all the files have been statted and their contents read
// into memory. If you need to do more operations per file, they should be chained in the map
// callback for concurrency.
}, {concurrency: concurrency}).call("sort", function(a, b) {
    return a.fileName.localeCompare(b.fileName);
}).then(function() {
    console.timeEnd("reading files");
});

Where is the concurrence limit? 并发限制在哪里?

Firstly your all request will go in to the event loop and then one by one in to callstack. 首先,您的所有请求都将进入事件循环 ,然后一个个地进入调用栈。 So there is no actual limit of this it will start eating your RAM when callstack started filling more and more items. 因此,对此没有实际限制,当调用堆栈开始填充越来越多的项目时,它将开始占用您的RAM。

How many calls can be made concurrently? 可以同时拨打多少个电话?

You can use series and parallel model - 您可以使用串联和并联模型-

Where you can to send 50(any number) request in one slot and then another slot and so on. 在这里您可以在一个插槽中发送50个(任意数量)请求,然后在另一个插槽中发送,依此类推。

To make series and parallel request you can use async series and async parallel respectively. 要进行串行和并行请求,可以分别使用异步串行异步并行

On what does it depends? 取决于什么?

Watch the link that I have mentioned in first answer. 观看我在第一个答案中提到的链接。

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

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