3

I am working with several APIs on my app and a few of them have limits that are not just simply per sec. For example one of my apis has the following limits:

Max 100 requests per 2 minutes
Max 20 requests per 1 second

So I have tried implementing this library https://github.com/aishek/axios-rate-limit in the following way:

axiosRateLimit(baseAxios.create(), {
  maxRequests: 1, // 1
  perMilliseconds: 1200 // per 1.2 seconds
  // 100 requests per 2 minutes, 50 requests per 60 seconds, 60 seconds / 50 requests = 1 per 1.2 seconds
});

But it can't take advantage of the 20 requests per 1 second limit, because to adhere to the 100 requests per 2 minutes, I have to limit it to 1 per 1.2 seconds, otherwise if I limit it to 20 per second, I can do 2400 requests in 2 minutes.

So how can I implement both conditions and have them both working together? What if I need to do only 50 requests every 2 minutes, with the current implementation, it will take me 1 minute for all of them, and I am not taking advantage of the 20 per second (becaus if I do, I can do it in 3 seconds, instead of 1 minute).

Is there a way to accomplish this with this library? Initially I thought that the maxRequests works with perMilliseconds and maxRPS can be used to handle the other case, so when all 3 are supplied I thought it would be like:

{
  maxRequests: 100, // 100 max requests
  perMilliseconds: 2 * 60 * 1000, // per 2 minutes
  maxRPS: 20 // 20 max per second
}

But the docs say:

// sets max 2 requests per 1 second, other will be delayed
// note maxRPS is a shorthand for perMilliseconds: 1000, and it takes precedence
// if specified both with maxRequests and perMilliseconds
const http = rateLimit(axios.create(), { maxRequests: 2, perMilliseconds: 1000, maxRPS: 2 })

So obviously it doesnt work the way I expected it to work, is there a way to achieve what I want?

Are there any other libraries that can do what I want?

Do I have to implement it from scratch on my own?

Darkbound
  • 3,026
  • 7
  • 34
  • 72
  • Did you figure it out in the end? I thought create a separate AxiosInstance for every API that has a specific rate limit. – CoderApprentice Dec 21 '22 at 11:27
  • 1
    @CoderApprentice I did exactly that, I created separate instances and calculated my limits down to the RPS so that they can be distributed evenly without going over the limit. Obviously not very optimal, not very optimal when every endpoint has different limit either (so I even created separate instnaces per endpoints, but thats ok since I use just a few), the general solution (which I will do at some point) is to write your own intereceptor for axios that can handle the limits by using the headers in the response to determine what is the limit for each endpoint – Darkbound Dec 22 '22 at 12:14
  • I see, I'm running into the same problem so I will probably do the same as you. Nice idea with the interceptors, didn't think of that one. But since I only use 2 endpoints I'll stick with the easier former solution :) – CoderApprentice Dec 22 '22 at 13:42

0 Answers0