MQ87
MQ87

Reputation: 1058

node.js : Currency limited parallel execution with time interval (rate limiting)

My goal is to send N request per time interval, and be sure that network lags won't effect on my algorithm. I made this function:

 
var results=[];

function sendBatchReq(docs, fromIndex, batchSize, timeout){

    var counter=batchSize;
    console.log(new Date()+ " executing");
    for (var i = fromIndex; i < fromIndex+batchSize; i++) { 

        var url=docs[i].url;

        request(url, function(err, res, data) {

            results.push(JSON.parse(data));

            counter--;

            if(counter==0 && docs.length >=fromIndex+(batchSize*2)){
                console.log(new Date()+" next batch");
                setTimeout(sendBatchReq(docs,fromIndex+batchSize,batchSize), timeout);
            }

        });

     }
}


sendBatchReq(docs, 0, 5, 10000);

This should work like this: Execute first 5 request, when the 5th cb is been executed launch another batch of 5 request after 10s.

The problem is that the timeout seems to don't work: all the new sendBathReq are executed just after console.log print out.

Upvotes: 0

Views: 140

Answers (1)

spender
spender

Reputation: 120548

You're invoking the function. You meant to pass a function, but you're passing the return value of that function because you accidentally called it inline. Did you mean:

setTimeout(function(){ sendBatchReq(docs, fromIndex + batchSize, batchSize);}, 
           timeout);

Upvotes: 2

Related Questions