I'm trying to combine TimeoutPolicy
and RetryPolicy
for a API call done in a Func
, but I don't found a way to achieve this.
If I only use the RetryPolicy
, it's working fine.
I've a GetRequest
method that call the HttpClient
and return the datas:
async Task<Data> GetRequest(string api, int id)
{
var httpClient = new HttpClient();
var response = await httpClient.GetAsync($"{api}{id}");
var rawResponse = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<Data>(rawResponse);
}
I also have the Func
that will embed the call to this method:
var func = new Func<Task<Data>>(() => GetRequest(api, i));
I call the service like this:
Results.Add(await _networkService.RetryWithoutTimeout<Data>(func, 3, OnRetry));
This RetryWithoutTimeout
method is like this:
async Task<T> RetryWithoutTimeout<T>(Func<Task<T>> func, int retryCount = 1, Func<Exception, int, Task> onRetry = null)
{
var onRetryInner = new Func<Exception, int, Task>((e, i) =>
{
return Task.Factory.StartNew(() => {
#if DEBUG
System.Diagnostics.Debug.WriteLine($"Retry #{i} due to exception '{(e.InnerException ?? e).Message}'");
#endif
});
});
return await Policy.Handle<Exception>()
.RetryAsync(retryCount, onRetry ?? onRetryInner)
.ExecuteAsync<T>(func);
}
I've updated this code to use a TimeoutPolicy
, with a new RetryWithTimeout
method:
async Task<T> RetryWithTimeout<T>(Func<Task<T>> func, int retryCount = 1, Func<Exception, int, Task> onRetry = null, int timeoutDelay = 30)
{
var onRetryInner = new Func<Exception, int, Task>((e, i) =>
{
return Task.Factory.StartNew(() => {
#if DEBUG
System.Diagnostics.Debug.WriteLine($"Retry #{i} due to exception '{(e.InnerException ?? e).Message}'");
#endif
});
});
var retryPolicy = Policy
.Handle<Exception>()
.RetryAsync(retryCount, onRetry ?? onRetryInner);
var timeoutPolicy = Policy.TimeoutAsync(TimeSpan.FromSeconds(timeoutDelay));
var policyWrap = timeoutPolicy.WrapAsync((IAsyncPolicy)retryPolicy);
return await policyWrap.ExecuteAsync(
async ct => await Task.Run(func),
CancellationToken.None
);
}
But I don't see how to manage the GetRequest()
method: all my tests have failed...
Edit: I've created a sample based on the @Peter Csala comment.
So first, I've just updated the number of retries to check if the retryPolicy
was correctly applied:
private const int TimeoutInMilliseconds = 2500;
private const int MaxRetries = 3;
private static int _times;
static async Task Main(string[] args)
{
try
{
await RetryWithTimeout(TestStrategy, MaxRetries);
}
catch (Exception ex)
{
WriteLine($"{nameof(Main)} - Exception - Failed due to: {ex.Message}");
}
Console.ReadKey();
}
private static async Task<string> TestStrategy(CancellationToken ct)
{
WriteLine($"{nameof(TestStrategy)} has been called for the {_times++}th times.");
await Task.Delay(TimeoutInMilliseconds * 2, ct);
return "Finished";
}
internal static async Task<T> RetryWithTimeout<T>(Func<CancellationToken, Task<T>> func, int retryCount = 1, Func<Exception, int, Task> onRetry = null, int timeoutDelay = TimeoutInMilliseconds)
{
WriteLine($"NetworkService - {nameof(RetryWithTimeout)}");
var onRetryInner = new Func<Exception, int, Task>((e, i) =>
{
WriteLine($"NetworkService - {nameof(RetryWithTimeout)} #{i} due to exception '{(e.InnerException ?? e).Message}'");
return Task.CompletedTask;
});
var retryPolicy = Policy
.Handle<Exception>()
.RetryAsync(retryCount, onRetry ?? onRetryInner);
var timeoutPolicy = Policy.TimeoutAsync(TimeSpan.FromMilliseconds(timeoutDelay));
var policyWrap = Policy.WrapAsync(retryPolicy, timeoutPolicy); //Important part #1
return await policyWrap.ExecuteAsync(
async ct => await func(ct), //Important part #2
CancellationToken.None);
}
Regarding the logs, it's well the case:
NetworkService - RetryWithTimeout
TestStrategy has been called for the 0th times.
NetworkService - RetryWithTimeout - Retry #1 due to exception 'A task was canceled.'
TestStrategy has been called for the 1th times.
NetworkService - RetryWithTimeout - Retry #2 due to exception 'A task was canceled.'
TestStrategy has been called for the 2th times.
NetworkService - RetryWithTimeout - Retry #3 due to exception 'A task was canceled.'
TestStrategy has been called for the 3th times.
Main - TimeoutRejectedException - Failed due to: The delegate executed asynchronously through TimeoutPolicy did not complete within the timeout.
Then, I've changed the policyWrap
as I need a global timeout:
private static async Task<string> TestStrategy(CancellationToken ct)
{
WriteLine($"{nameof(TestStrategy)} has been called for the {_times++}th times.");
await Task.Delay(1500, ct);
throw new Exception("simulate Exception");
}
var policyWrap = timeoutPolicy.WrapAsync(retryPolicy);
Regarding the logs, it's also correct:
TestStrategy has been called for the 0th times.
NetworkService - RetryWithTimeout #1 due to exception 'simulate Exception'
TestStrategy has been called for the 1th times.
NetworkService - RetryWithTimeout #2 due to exception 'A task was canceled.'
Main - TimeoutRejectedException - Failed due to: The delegate executed asynchronously through TimeoutPolicy did not complete within the timeout.
After that, I've implemented a method that call an API, with some Exceptions
, to be more close to my need:
static async Task Main(string[] args)
{
try
{
await RetryWithTimeout(GetClientAsync, MaxRetries);
}
catch (TimeoutRejectedException trEx)
{
WriteLine($"{nameof(Main)} - TimeoutRejectedException - Failed due to: {trEx.Message}");
}
catch (WebException wEx)
{
WriteLine($"{nameof(Main)} - WebException - Failed due to: {wEx.Message}");
}
catch (Exception ex)
{
WriteLine($"{nameof(Main)} - Exception - Failed due to: {ex.Message}");
}
Console.ReadKey();
}
private static async Task<CountriesResponse> GetClientAsync(CancellationToken ct)
{
WriteLine($"{nameof(GetClientAsync)} has been called for the {_times++}th times.");
HttpClient _client = new HttpClient();
try
{
var response = await _client.GetAsync(apiUri, ct);
// ! The server response is faked through a Proxy and returns 500 answer !
if (!response.IsSuccessStatusCode)
{
WriteLine($"{nameof(GetClientAsync)} - !response.IsSuccessStatusCode");
throw new WebException($"No success status code {response.StatusCode}");
}
var rawResponse = await response.Content.ReadAsStringAsync();
WriteLine($"{nameof(GetClientAsync)} - Finished");
return JsonConvert.DeserializeObject<CountriesResponse>(rawResponse);
}
catch (TimeoutRejectedException trEx)
{
WriteLine($"{nameof(GetClientAsync)} - TimeoutRejectedException : {trEx.Message}");
throw trEx;
}
catch (WebException wEx)
{
WriteLine($"{nameof(GetClientAsync)} - WebException: {wEx.Message}");
throw wEx;
}
catch (Exception ex)
{
WriteLine($"{nameof(GetClientAsync)} - other exception: {ex.Message}");
throw ex;
}
}
The logs are still correct:
NetworkService - RetryWithTimeout
GetClientAsync has been called for the 0th times.
GetClientAsync - !response.IsSuccessStatusCode
GetClientAsync - WebException: No success status code InternalServerError
NetworkService - RetryWithTimeout #1 due to exception 'No success status code InternalServerError'
GetClientAsync has been called for the 1th times.
GetClientAsync - !response.IsSuccessStatusCode
GetClientAsync - WebException: No success status code InternalServerError
NetworkService - RetryWithTimeout #2 due to exception 'No success status code InternalServerError'
GetClientAsync has been called for the 2th times.
GetClientAsync - !response.IsSuccessStatusCode
GetClientAsync - WebException: No success status code InternalServerError
NetworkService - RetryWithTimeout #3 due to exception 'No success status code InternalServerError'
GetClientAsync has been called for the 3th times.
GetClientAsync - other exception: The operation was canceled.
Main - TimeoutRejectedException - Failed due to: The delegate executed asynchronously through TimeoutPolicy did not complete within the timeout.
Finally, I would like to be able to call a "generic" method, that I could reuse for each API call. This method will be like this:
static async Task<T> ProcessGetRequest<T>(Uri uri, CancellationToken ct)
{
WriteLine("ApiService - ProcessGetRequest()");
HttpClient _client = new HttpClient();
var response = await _client.GetAsync(uri);
if (!response.IsSuccessStatusCode)
{
WriteLine("ApiService - ProcessGetRequest() - !response.IsSuccessStatusCode");
throw new WebException($"No success status code {response.StatusCode}");
}
var rawResponse = await response.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<T>(rawResponse);
}
But for this, I have to pass at the same time the CancellationToken
and the Api Uri
through the the RetryWithTimeout
and I don't see how to manage this.
I've tried to change the signature of RetryWithTimeout
by something like:
internal static async Task<T> RetryWithTimeout<T>(Func<Uri, CancellationToken, Task<T>> func, int retryCount = 1, Func<Exception, int, Task> onRetry = null, int timeoutDelay = TimeoutInMilliseconds)
But I don't find how to the manage the Func
...
Would you have an idea or an explanation?
I finally found a solution that works, this solution has been completed by @Peter Csala.
I get these results:
=> the API calls are retried until there is the Timeout
Thank you @Peter Csala!