Here’s the translation of the Go rate limiting example to Swift, formatted in Markdown suitable for Hugo:
Rate limiting is an important mechanism for controlling resource utilization and maintaining quality of service. Swift supports rate limiting through the use of Grand Central Dispatch (GCD) and timers.
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.
This example demonstrates how to implement rate limiting in Swift using Grand Central Dispatch (GCD) and semaphores. The concepts are similar to the original example, but adapted to Swift’s concurrency model.