Polly icon indicating copy to clipboard operation
Polly copied to clipboard

RateLimiting with RetryAfter not compiling

Open Queenferno opened this issue 4 years ago • 5 comments

Greetings all. I'm trying to create a RateLimiter using the examples provided here: https://github.com/App-vNext/Polly#rate-limit

Specifically, I'm trying to build a Policy using the (retryAfter, context) predicate as stated in the example:

// Allow up to 20 executions per second with a burst of 10 executions,
// with a delegate to return the retry-after value to use if the rate
// limit is exceeded.
Policy.RateLimit(20, TimeSpan.FromSeconds(1), 10, (retryAfter, context) =>
{
    return retryAfter.Add(TimeSpan.FromSeconds(2));
});

This is my code and it doesn't compile because:

  • The Policy.RateLimit must return a IAsyncPolicy
  • I'm returning an IAsyncPolicy-TimeSpan

This is my code:

return Policy .HandleResult<HttpResponseMessage>(result => { return result.StatusCode == System.Net.HttpStatusCode.TooManyRequests; }) .Or<Polly.RateLimit.RateLimitRejectedException>() .WaitAndRetryForeverAsync((retryNum) => { Console.WriteLine($"Retrying. Num: {retryNum}"); return TimeSpan.FromTicks(1); }).WrapAsync( Policy.RateLimitAsync(mps, TimeSpan.FromSeconds(1), mps, (retryAfter, context) => { return retryAfter.Add(TimeSpan.FromSeconds(2)); }));

Is this a bug, or am I writing my policy incorrectly?

Queenferno avatar May 27 '22 07:05 Queenferno

*edit (for some reason i cannot edit my initial issue) --> The Policy.RateLimitAsync must return a IAsyncPolicy and I'm returning an IAsyncPolicy-TimeSpan

Queenferno avatar May 27 '22 07:05 Queenferno

  • If you call the RateLimitAsync without the retryAfterFactory parameter then the return type will be AsyncRateLimitPolicy
  • If you call the RateLimitAsync with the retryAfterFactory parameter then the return type will be AsyncRateLimitPolicy<TResult>

So, if you use the latter one the returned type will implement IAsyncPolicy<TResult> and not IAsyncPolicy

PeterCsalaHbo avatar May 27 '22 08:05 PeterCsalaHbo

Can you post a code-example? I don't know how to do that. (THanx for your reply)

Queenferno avatar May 27 '22 08:05 Queenferno

Let me try to rephrase it. If you want to / need to provide retyrAfterFactory you can't return with an IAsyncPolicy.

So, you can't combine an IAsyncPolicy<HttpResponseMessage> (your retry) with an IAsyncPolicy<TimeSpan> (your ratelimit)

IAsyncPolicy<TimeSpan> limit = Policy
    .RateLimitAsync(mps, TimeSpan.FromSeconds(1), mps,
        (retryAfter, context) => retryAfter.Add(TimeSpan.FromSeconds(2)));

IAsyncPolicy<HttpResponseMessage> retry =  Policy
    .HandleResult<HttpResponseMessage>(result => result.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
    .Or<RateLimitRejectedException>()
    .WaitAndRetryForeverAsync((retryNum) => { Console.WriteLine($"Retrying. Num: {retryNum}"); return TimeSpan.FromTicks(1); });

var resilienceStrategy = Policy.WrapAsync<HttpResponseMessage>(retry, limit); //limit is not compatible

If you want to make it compatible then you need to change the return type of the rate limiter to HttpResponseMessage something like this

IAsyncPolicy<HttpResponseMessage> limit = Policy
    .RateLimitAsync(mps, TimeSpan.FromSeconds(1), mps,
        (retryAfter, context) => {
            var response = new HttpResponseMessage(System.Net.HttpStatusCode.TooManyRequests);
            response.Headers.Add("Retry-After", retryAfter.Add(TimeSpan.FromSeconds(2)).TotalSeconds.ToString());
            return response;
        });

IAsyncPolicy<HttpResponseMessage> retry =  Policy
    .HandleResult<HttpResponseMessage>(result => result.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
    .Or<RateLimitRejectedException>()
    .WaitAndRetryForeverAsync((retryNum) => { Console.WriteLine($"Retrying. Num: {retryNum}"); return TimeSpan.FromTicks(1); });

var resilienceStrategy = Policy.WrapAsync(retry, limit);

PeterCsalaHbo avatar May 27 '22 08:05 PeterCsalaHbo

Thank you! I applied the second snippet and it worked like a charm!

Queenferno avatar May 27 '22 11:05 Queenferno