Polly:如何结合 TimeoutPolicy 和 RetryPolicy 来请求 Func

Polly: how to combine TimeoutPolicy and RetryPolicy to request a Func

我正在尝试将 TimeoutPolicyRetryPolicy 结合起来用于 Func 中完成的 API 调用,但我没有找到实现此目的的方法.

如果我只使用 RetryPolicy,它工作正常。

我有一个 GetRequest 方法调用 HttpClient 和 return 数据:

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);
}

我还有 Func 将嵌入对此方法的调用: var func = new Func<Task<Data>>(() => GetRequest(api, i));

我这样调用服务: Results.Add(await _networkService.RetryWithoutTimeout<Data>(func, 3, OnRetry));

这个RetryWithoutTimeout方法是这样的:

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);
}

我已经更新此代码以使用 TimeoutPolicy,以及新的 RetryWithTimeout 方法:

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
                    );
}

但是我不知道如何管理 GetRequest() 方法:我所有的测试都失败了...

编辑: 我根据@Peter Csala 的评论创建了一个示例。

所以首先,我刚刚更新了重试次数以检查retryPolicy是否正确应用:

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);
}

关于日志,是这样的:

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.

然后,我更改了 policyWrap 因为我需要全局超时:

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);

关于日志,也是正确的:

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.

在那之后,我实现了一个调用 API 的方法,其中一些 Exceptions 更接近我的需要:

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;
    }
}

日志仍然正确:

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.

最后,我希望能够调用一个“通用”方法,我可以在每个 API 调用中重复使用该方法。这个方法将是这样的:

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);
}

但是为此,我必须同时通过 CancellationToken 和 Api Uri 通过 RetryWithTimeout 我不知道如何来管理这个。

我尝试通过类似以下方式更改 RetryWithTimeout 的签名:

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)

但我找不到如何管理 Func...

你有什么想法或解释吗?

您需要将 CancellationToken 传递给 to-be-cancelled(由于超时)函数。

那么,假设您有以下简化方法:

private const int TimeoutInMilliseconds = 1000;
private static int _times;
private static async Task<string> TestStrategy(CancellationToken ct)
{
    Console.WriteLine($"{nameof(TestStrategy)} has been called for the {_times++}th times.");
    await Task.Delay(TimeoutInMilliseconds * 2, ct);
    return "Finished";
}

因此,您的 RetryWithTimeout 可以这样调整/修改:

static async Task<T> RetryWithTimeout<T>(Func<CancellationToken, Task<T>> func, int retryCount = 1, Func<Exception, int, Task> onRetry = null, int timeoutDelay = TimeoutInMilliseconds)
{
    var onRetryInner = new Func<Exception, int, Task>((e, i) =>
    {
        Console.WriteLine($"Retry #{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);
}

重要部分 #1 - 重试是外部策略,超时是内部策略
重要部分 #2 - 由于超时

,CancellationToken 被传递给 to-be-cancelled 函数

下面的用法

static async Task Main(string[] args)
{
    try
    {
        await RetryWithTimeout(TestStrategy);
    }
    catch (Exception ex)
    {
        Console.WriteLine($"Failed due to: {ex.Message}");
    }

    Console.ReadKey();
}

将产生以下输出:

TestStrategy has been called for the 0th times.
Retry #1 due to exception 'A task was canceled.'
TestStrategy has been called for the 1th times.
Failed due to: The delegate executed asynchronously through TimeoutPolicy did not complete within the timeout.

请记住,在重试开始之前有第 0 次尝试。

我终于找到了一个可行的解决方案,这个解决方案已由@Peter Csala 完成。

private const int TimeoutInMilliseconds = 2500;
private const int MaxRetries = 3;

private static Uri apiUri = new Uri("https://api/param");
private static int _times;

public static Country[] Countries
{
    get;
    set;
}

static async Task Main(string[] args)
{
    try
    {
        await LoadCountriesWithRetry(false);
    }
    catch (Exception ex)
    {
        WriteLine($"{nameof(Main)} - Exception - Failed due to: {ex.Message}");
    }
    Console.ReadKey();
}

static async Task LoadCountriesWithRetry(bool shouldWaitAndRetry)
{
    WriteLine($"{nameof(LoadCountriesWithRetry)}");
    try
    {
        Countries = await GetCountriesWithRetry();
    }
    catch (TimeoutRejectedException trE)
    {
        WriteLine($"{nameof(LoadCountriesWithRetry)} - TimeoutRejectedException : {trE.Message}");
    }   
    catch (WebException wE)
    {
        WriteLine($"{nameof(LoadCountriesWithRetry)} - WebException : {wE.Message}");
    }
    catch (Exception e)
    {
        WriteLine($"{nameof(LoadCountriesWithRetry)} - Exception : {e.Message}");
    }
}

public static async Task<Country[]> GetCountriesWithRetry()
{
    WriteLine($"{nameof(GetCountriesWithRetry)}");
    var response = await GetAndRetry<CountriesResponse>(uri, MaxRetries);
    return response?.Countries;
}

static Func<CancellationToken, Task<T>> IssueRequest<T>(Uri uri) => ct => ProcessGetRequest<T>(ct, uri);

public static async Task<T> GetAndRetry<T>(Uri uri, int retryCount, Func<Exception, int, Task> onRetry = null)
    where T : class
{
    WriteLine($"{nameof(GetAndRetry)}");
    return await RetryWithTimeout<T>(IssueRequest<T>(uri), retryCount);
}

static async Task<T> ProcessGetRequest<T>(CancellationToken ct, Uri uri)
{
    WriteLine($"{nameof(ProcessGetRequest)}");
    HttpClient _client = new HttpClient();
    var response = await _client.GetAsync(uri, ct);
    if (!response.IsSuccessStatusCode)
    {
        WriteLine($"{nameof(ProcessGetRequest)} - !response.IsSuccessStatusCode");
        throw new WebException($"No success status code {response.StatusCode}");
    }
    var rawResponse = await response.Content.ReadAsStringAsync();
    WriteLine($"{nameof(ProcessGetRequest)} - Success");
    return JsonConvert.DeserializeObject<T>(rawResponse);
}

internal static async Task<T> RetryWithTimeout<T>(Func<CancellationToken, Task<T>> func, Uri uri, int retryCount = 1, Func<Exception, int, Task> onRetry = null, int timeoutDelay = TimeoutInMilliseconds)
{
    WriteLine($"{nameof(RetryWithTimeout)}");
    var onRetryInner = new Func<Exception, int, Task>((e, i) =>
    {
        WriteLine($"{nameof(RetryWithTimeout)} - onRetryInner #{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 = timeoutPolicy.WrapAsync(retryPolicy);

    return await policyWrap.ExecuteAsync(
                    async (ct) => await func(ct),
                    CancellationToken.None);
}

我得到这些结果:

GetCountriesWithRetry
GetAndRetry
RetryWithTimeout
ProcessGetRequest
ProcessGetRequest - !response.IsSuccessStatusCode
RetryWithTimeout - onRetryInner #1 due to exception 'No success status code InternalServerError'
ProcessGetRequest
ProcessGetRequest - !response.IsSuccessStatusCode
RetryWithTimeout - onRetryInner #2 due to exception 'No success status code InternalServerError'
LoadCountriesWithRetry - TimeoutRejectedException : The delegate executed asynchronously through TimeoutPolicy did not complete within the timeout.

=> 重试 API 调用,直到超时

谢谢@Peter Csala!