I’ve been coming back to the same project a few times. It’s essentially just a program that interacts with an API. Only problem is whenever I get back to it, I realize how annoying it is to debug through all the “too many requests” responses I get back from the API because it has a max of 200 requests per second.

On solution would be to filter out those responses but that just feels like the wrong move, so I’m guessing the better solution would be to put some sort of rate limiter on my program. My two questions are: does that seem like a good solution and if it is, do I embed the rate limiter in my program, i.e. using the ratelimit crate or would a better solution be to run my program in a container and connect it to a reverse proxy(I think) container and control rate limiting from there?

  • kn33@lemmy.world
    link
    fedilink
    English
    arrow-up
    2
    ·
    edit-2
    1 day ago

    I don’t know Rust, I’m just here to chill. I can tell you what I would do, and have done, in PowerShell to solve this. From there you can translate that to Rust.

    Let’s go with your limit of 200 requests per second. At the start of the script, I create a stopwatch. Literally, a stopwatch that’s tied to real world time and can be reset. Then, I have a variable that counts my requests. Every time I make a request, I increment it. Before every request, I check if the variable is 200. If it is, check the timer to see if a second has passed. If not, calculate how much time is left until a second has passed and sleep for that amount of time. After doing that check/sleep, reset the request counter and the stopwatch. From there, continue on.