I have a RabbitMQ Queue, filled with thousands of messages. I need my consumer to consume 1 message per second, so I have implemented a RateLimit policy using Polly. My configuration is as follows:
public static IAsyncPolicy GetPolicy(int mps)
{
if (mps <= 0)
{
throw new ArgumentOutOfRangeException(nameof(mps));
}
return Policy
.HandleResult<HttpResponseMessage>(result => {
return result.StatusCode == System.Net.HttpStatusCode.TooManyRequests;
})
.Or<Polly.RateLimit.RateLimitRejectedException>()
.WaitAndRetryForeverAsync((retryNum, context) => {
Console.WriteLine($"Retrying. Num: {retryNum}");
return TimeSpan.FromSeconds(1);
}).WrapAsync(
Policy.RateLimitAsync(mps, TimeSpan.FromSeconds(1)));
}
where mps
is 1
Now what I've noticed is the following:
- In the beginning, 50 messages are consumed from my Queue, in a span of 1 second. RateLimiter looks like not working
- Then, one message per second is consumed, with
WaitAndRetryForeverAsync
executing multiple (tens) of times
If I set the mps
to 50, the following happens:
- In the beginning 50 messages are immediately consumed
- Then 20 messages per second are consumed (and not 50 as expected)
Is there a bug with the Policy.RateLimitAsync
call?
Am I doing something wrong?