Billy Moon
Billy Moon

Reputation: 58521

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

Upvotes: 2

Views: 3901

Answers (5)

Wachid
Wachid

Reputation: 617

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.

Upvotes: 0

Jonathan Ong
Jonathan Ong

Reputation: 20315

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

Upvotes: 2

jfriend00
jfriend00

Reputation: 707158

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

Upvotes: 5

Oh My Goodness
Oh My Goodness

Reputation: 216

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

Upvotes: 1

Amadan
Amadan

Reputation: 198304

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>

Upvotes: 2

Related Questions