I'm trying to achieve fine-grained throttling with aiohttp.
By fine-grained, I mean that controlling how many requests are simultaneously executed is not enough, I want to rate-limit the number of requests to match 100 requests per 10 seconds interval.
The interesting part resides in throttled_coroutine
which acquires/releases a lock at a speed which is exactly the rate-limit I want to enforce by either:
- waiting a delay once requests performing very quickly are completed
- releasing lock before completion of requests performing poorly
At the end of a batch of requests, last coroutines tend to wait for no reason. This behaviour amplifies when timeout increases.
How should I deal with it? (see FIXME) I've considered looking for semaphore._waiters, but it seems like a bad choice because having no waiting at a particular point in times does not guarantee we are at the end of the batch.
Other than that, I'd be glad to have some feedback regarding my try/excecpt block and the lack of with statement in throttled_coroutine