I have a method that does some IO, and I'd like to limit the calls (per second) to this method to avoid the backend to get bursts of concurrent requests it can't handle.
If the requirement came without the "per second" I could just use a stack (basically just a counter) and offer()
when starting a request, and poll()
when done. With the "per second" requirement I'd somehow need to clean slots on the stack that are older than a given lapse of time.
How do I do that correctly? The structure should be thread-safe, obviously.
Thank you for your time!