如何限制api请求的堆栈?

时间:2016-02-16 02:01:39

标签: javascript node.js promise underscore.js throttling

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

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

_.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));
        });
});

5 个答案:

答案 0 :(得分:3)

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

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();
}

答案 1 :(得分:1)

使用Bluebird:

&#13;
&#13;
function fetch(id) {
  console.log("Fetching " + id);
  return Promise.delay(2000, id)
  .then(function(id) {
    console.log(" Fetched " + id);
  });
}

var ids = [1,2,3,4,5,6,7,8,9,10];
Promise.map(ids, fetch, { concurrency: 3 });
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/bluebird/3.3.1/bluebird.min.js"></script>

<!-- results pane console output; see http://meta.stackexchange.com/a/242491 -->
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

将数据划分为您希望并发连接的数组。使用setTimeout计划,并使用完成回调处理子数组的其余部分。

将setTimeout包装在自己的函数中,以便在delayed_fetch()调用时将变量值冻结为它们的值。

 WebElement element = driver.findElement(By.className(".enter_pl"));
 JavascriptExecutor executor = (JavascriptExecutor)driver;
 executor.executeScript("arguments[0].click();", element);

答案 3 :(得分:0)

我建议仅使用喉咙:https://github.com/ForbesLindesay/throat

答案 4 :(得分:0)

使用蓝鸟

function getUserFunc(user) {
  //Get a collection of user
}

function getImageFunc(id) {
  //get a collection of image profile based on id of the user
}

function search(response) {
  return getUsersFunc(response).then(response => {
    const promises = response.map(items => return items.id);
    const images = id => {
        return getImagesFunc(id).then(items => items.image);
    };
    return Promise.map(promises, images, { concurrency: 5 });
  });
}

以前,我使用过ES6函数Promise.all(),但是它不能像我期望的那样工作。然后使用第三方库bluebird.js并像魅力一样工作。