如何限制api请求的堆栈? [英] How can I throttle stack of api requests?

查看:98
本文介绍了如何限制api请求的堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个id数组,我想为每个id做一个api请求,但我想控制每秒发出多少请求,或者更好的是,在任何时候只有5个打开的连接,以及何时连接完成后,获取下一个。

I have an array of ids, and I want to make an api request for each id, but I want to control how many requests are made per second, or better still, have only 5 open connections at any time, and when a connection is complete, fetch the next one.

目前我有这个,它只是同时触发所有请求:

Currently I have this, which just fires off all the requests at the same time:

_.each([1,2,3,4,5,6,7,8,9,10], function(issueId) {
    github.fetchIssue(repo.namespace, repo.id, issueId, filters)
        .then(function(response) {
            console.log('Writing: ' + issueId);
            writeIssueToDisk(fetchIssueCallback(response));
        });
});


推荐答案

就个人而言,我会使用Bluebird的 .map()使用并发选项,因为我已经使用promises和Bluebird进行任何异步。但是,如果你想看看什么是手动编码的计数器方案,它限制了一次可以运行的并发请求的数量,那么就是一个:

Personally, I'd use Bluebird's .map() with the concurrency option since I'm already using promises and Bluebird for anything async. But, if you want to see what a hand-coded counter scheme that restricts how many concurrent requests can run at once looks like, here's one:

function limitEach(collection, max, fn, done) {
    var cntr = 0, index = 0, errFlag = false;

    function runMore() {
        while (!errFlag && cntr < max && index < collection.length) {
            ++cntr;
            fn(collection[index++], function(err, data) {
                --cntr;
                if (errFlag) return;
                if (err) {
                    errFlag = true;
                    done(err);
                } else {
                   runMore();
                }
            });
        }
        if (!errFlag && cntr === 0 && index === collection.length) {
            done();
        }
    }
    runMore();
}

这篇关于如何限制api请求的堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆