RateLimiting with RetryAfter not compiling
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?
*edit (for some reason i cannot edit my initial issue) --> The Policy.RateLimitAsync must return a IAsyncPolicy and I'm returning an IAsyncPolicy-TimeSpan
- If you call the
RateLimitAsyncwithout theretryAfterFactoryparameter then the return type will beAsyncRateLimitPolicy - If you call the
RateLimitAsyncwith theretryAfterFactoryparameter then the return type will beAsyncRateLimitPolicy<TResult>
So, if you use the latter one the returned type will implement IAsyncPolicy<TResult> and not IAsyncPolicy
Can you post a code-example? I don't know how to do that. (THanx for your reply)
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);
Thank you! I applied the second snippet and it worked like a charm!