我如何限制 api 请求的堆栈?

How can I throttle stack of api requests?

我有一个 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));
        });
});

与蓝鸟:

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

根据需要并发连接数将数据分成多个数组。使用 setTimeout 进行调度,并让完成回调处理子数组的其余部分。

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

function delayed_fetch(delay, namespace, id, issueIds, filters) {
  setTimeout(
    function() { 
      var issueId=issueIds.shift();
      github.fetchIssue(namespace, id, issueId, filters).then(function(response) {
        console.log('Writing: ' + issueId);
        writeIssueToDisk(fetchIssueCallback(response));
        delayed_fetch(0, namespace, id, issueIds, filters);
      });
    }, delay);
}

var i=0;
_.each([ [1,2] , [3,4], [5,6], [7,8], [9,10] ], function(issueIds) {
        var delay=++i*200; // millisecond
        delayed_fetch(delay, repo.namespace, repo.id, issueIds, filters);
});

就个人而言,我会使用 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();
}

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

使用蓝鸟

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 并像魅力一样工作。