简体   繁体   中英

How can I throttle stack of api requests?

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

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

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> 

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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