Anand Sunderraman
Anand Sunderraman

Reputation: 8148

Node.js controlling no of post calls per second

I am subscribing to an inhouse api (POST request) that restricts me to make 5 calls per second.

node.js is being used to make these rest api calls.

How to write code to restrict the no. of api calls invoked per second ?

Upvotes: 0

Views: 104

Answers (1)

seymar
seymar

Reputation: 4063

Use setInterval(makeAPICall, 200); to execute the API calls every 200 milliseconds.

Every execution of the makeAPICall function you make one call from a queue.

var queue = [
    {
        arg1 : 'arg1value',
        arg2 : 'arg2value'
    },
    {
        arg1 : 'arg1value',
        arg2 : 'arg2value'
    }
];

setInterval(function() {
    var arguments = queue[0];

    // API call function, using arguments
    makeAPICall(arguments.arg1, arguments.arg2);

    // Remove from queue
    queue.splice(0, 1);
}, 200);

Upvotes: 2

Related Questions