How can I throttle stack of api requests?

╄→гoц情女王★ 提交于 2019-12-01 12:26:33

问题


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

回答1:


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



回答2:


With Bluebird:

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>



回答3:


Divide your data into as many arrays as you want concurrent connections. Schedule with setTimeout, and have the completion callback handle the rest of the sub-array.

Wrap the setTimeout in a function of its own so that the variable values are frozen to their values at the time of delayed_fetch() invocation.

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



回答4:


i'd recommend using throat just for this: https://github.com/ForbesLindesay/throat




回答5:


Using Bluebird

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

Previously i used ES6 function Promise.all(), but it doesn't work like what i'm expecting. Then go with third party library bluebird.js and Work like a charm.



来源:https://stackoverflow.com/questions/35422377/how-can-i-throttle-stack-of-api-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!