C#C
C#4y ago
Doombox

Using multiple RateLimiters correctly. [Answered]

I'm wrapping an API which requires no more than 4 requests per second and 200 per hour, I've got a setup like this
_hourlyRateLimiter = new FixedWindowRateLimiter(new FixedWindowRateLimiterOptions(
    200, QueueProcessingOrder.OldestFirst, 1, TimeSpan.FromHours(1)));
_secondRateLimiter = new FixedWindowRateLimiter(new FixedWindowRateLimiterOptions(
    4, QueueProcessingOrder.OldestFirst, 1, TimeSpan.FromSeconds(1)));
// ...
public async Task Test(int testNumber)
{
    var hourly = _hourlyRateLimiter.WaitAsync();
    var second = _secondRateLimiter.WaitAsync();
    await ValueTaskExtensions.WhenAll(hourly, second);
    Console.WriteLine(testNumber);
}

this works well if you await each call of Test, however if you try something like this...
var tasks = new List<Task>();
for (var i = 0; i < 250; i++)
    tasks.Add(Test(i));
await Task.WhenAll(tasks);

the rate limiter seems to break, have I written this poorly or is this just an issue with concurrency in the library?
Was this page helpful?