I'm using Polly with .net Core. My ConfigureServices
is :
private static void ConfigureServices()
{
var collection = new ServiceCollection();
var timeoutPolicy = Policy.TimeoutAsync<HttpResponseMessage>(3);
collection.AddHttpClient<INetworkService, NetworkService>(s=>
{
s.BaseAddress = new Uri("http://google.com:81"); //this is a deliberate timeout url
})
.AddPolicyHandler((a,b)=>GetRetryPolicy(b))
.AddPolicyHandler(timeoutPolicy); ;
...
}
This is the GetRetryPolicy
function:
private static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy(HttpRequestMessage req)
{
return HttpPolicyExtensions
.HandleTransientHttpError()
.OrResult(msg => httpStatusCodesWorthRetrying.Contains(msg.StatusCode)) // see below
.Or<TimeoutRejectedException>()
.Or<TaskCanceledException>()
.Or<OperationCanceledException>()
.WaitAndRetryAsync(3, retryAttempt =>
{
return TimeSpan.FromSeconds(3);
}, onRetry: (response, delay, retryCount, context) =>
{
Console.WriteLine($"______PollyAttempt_____ retryCount:{retryCount}__FOR_BaseUrl_{req.RequestUri.ToString()}");
});
}
Those are the httpcodes I want to retry :
static HttpStatusCode[] httpStatusCodesWorthRetrying = {
HttpStatusCode.RequestTimeout, // 408
HttpStatusCode.InternalServerError, // 500
HttpStatusCode.BadGateway, // 502
HttpStatusCode.ServiceUnavailable, // 503
HttpStatusCode.GatewayTimeout // 504
};
Ok. And this is the actual invocation :
public async Task Work()
{
try
{
HttpResponseMessage response = await _httpClient.GetAsync("");
Console.WriteLine("After work");
}
catch (TimeoutRejectedException ex)
{
Console.WriteLine("inside TimeoutRejectedException");
}
catch (Exception ex)
{
Console.WriteLine("inside catch main http");
}
}
The output is :
_PollyAttempt retryCount:1__FOR_BaseUrl_http://google.com:81/
_PollyAttempt retryCount:2__FOR_BaseUrl_http://google.com:81/
_PollyAttempt retryCount:3__FOR_BaseUrl_http://google.com:81/
inside TimeoutRejectedException
(notice it throws) Which is OK. Because Polly throws after this invalid URL is timeout.
But if I change the http://google.com:81/
to an "internal server error" url : (this return 500)
https://run.mocky.io/v3/9f1b4c18-2cf0-4303-9136-bb67d54d0148
Then it doesn't throw but continues :
_PollyAttempt retryCount:1__FOR_BaseUrl_https://run.mocky.io/v3/9f1b4c18-2cf0-4303-9136-bb67d54d0148
_PollyAttempt retryCount:2__FOR_BaseUrl_https://run.mocky.io/v3/9f1b4c18-2cf0-4303-9136-bb67d54d0148
_PollyAttempt retryCount:3__FOR_BaseUrl_https://run.mocky.io/v3/9f1b4c18-2cf0-4303-9136-bb67d54d0148
After work
(notice "after work" at the end)
Question:
Why does Polly throw at timeout, But doesn't throw at another condition ?
I explictly wrote : .OrResult(msg => httpStatusCodesWorthRetrying.Contains(msg.StatusCode))
and 500 is one of them.
OrResult
part is not needed. TheHandleTransientFailure
covers 408 and 5xx status codes. – AshlieashlinTimeoutRejectedException
was handled in the correct way as well. In theWork
you are catching it and that means Retry policy won't be triggered because ofTimeoutRejectedException
. – AshlieashlinPolicyWrap
to avoid this kind of confusion. – Ashlieashlin