Observing rate limits with coroutines in Koltin clients
When performing batch operations against a resource enforcing a rate limit it is necessary to observe that rate limit to ensure the batch does not fail with a HTTP 429 response.
Kotlin coroutines provide a succinct way to observe limits. The solution rests on the fact that the parent of a coroutine will only complete once all its children have completed.
In this snippet a delay is fired off in parallel to the expensive operation. The delay is the rate limit expressed as one request per X milliseconds. The effect is that observeRateLimitAsync()
will take the maximum of the delay or the execution time of the expensive operation to complete and thus will never exceed the rate limit. It also won’t waste any time delaying for longer than necessary.