Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Throttle and queue up API requests due to per second cap

I'm use mikeal/request to make API calls. One of the API's I use most frequently (the Shopify API). Recently put out a new call limit, I'm seeing errors like:

Exceeded 6.0 calls per second for api client. Slow your requests or contact support for higher limits. 

I've already gotten an upgrade, but regardless of how much bandwidth I get I have to account for this. A large majority of the requests to the Shopify API are within async.map() functions, which loop asynchronous requests, and gather the bodies.

I'm looking for any help, perhaps a library that already exists, that would wrap around the request module and actually block, sleep, throttle, allocate, manage, the many simultaneous requests that are firing off asynchronously and limit them to say 6 requests at a time. I have no problem with working on such a project if it doesn't exist. I just don't know how to handle this kind of situation, and I'm hoping for some kind of standard.

I made a ticket with mikeal/request.

like image 314
ThomasReggi Avatar asked Nov 27 '13 21:11

ThomasReggi


People also ask

What is API request throttling?

What is API Throttling? API throttling allows you to control the way an API is used. Throttling allows you to set permissions as to whether certain API calls are valid or not. Throttles indicate a temporary state, and are used to control the data that clients can access through an API.

How do you deal with a throttling API?

Best practices to handle throttling The following are best practices for handling throttling: Reduce the degree of parallelism. Reduce the frequency of calls. Avoid immediate retries because all requests accrue against your usage limits.

How do I throttle API request?

One way to implement API throttling in distributed systems is to use sticky sessions. In this method, all requests from a user are always serviced by a particular server. However, this solution is not well-balanced or fault tolerant. The second solution to API throttling in distributed systems are locks.

What does it mean to throttle requests?

Throttling is the process of limiting the number of requests you (or your authorized developer) can submit to a given operation in a given amount of time.


1 Answers

For an alternative solution, I used the node-rate-limiter to wrap the request function like this:

var request = require('request'); var RateLimiter = require('limiter').RateLimiter;  var limiter = new RateLimiter(1, 100); // at most 1 request every 100 ms var throttledRequest = function() {     var requestArgs = arguments;     limiter.removeTokens(1, function() {         request.apply(this, requestArgs);     }); }; 
like image 104
Dmitry Chornyi Avatar answered Oct 03 '22 04:10

Dmitry Chornyi