Here’s the translation of the Go rate limiting example to Ruby:
Rate limiting is an important mechanism for controlling resource utilization and maintaining quality of service. Ruby supports rate limiting through threads and queues.
Running our program we see the first batch of requests handled once every ~200 milliseconds as desired.
For the second batch of requests we serve the first 3 immediately because of the burstable rate limiting, then serve the remaining 2 with ~200ms delays each.
In this Ruby version, we use Queue and SizedQueue to implement the rate limiting behavior. Threads are used to simulate the ticking behavior and to run the main logic concurrently. The sleep method is used to introduce delays, and Time.now is used to get the current time for logging purposes.