我正在使用 .net Core 的 Polly。我的 ConfigureServices
如下:
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); ;
...
}
以下是 GetRetryPolicy
函数:
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()}");
});
}
这些是我想要重试的HTTP状态码:
static HttpStatusCode[] httpStatusCodesWorthRetrying = {
HttpStatusCode.RequestTimeout, // 408
HttpStatusCode.InternalServerError, // 500
HttpStatusCode.BadGateway, // 502
HttpStatusCode.ServiceUnavailable, // 503
HttpStatusCode.GatewayTimeout // 504
};
好的。这是实际调用:
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");
}
}
输出结果为:
_PollyAttempt 重试次数:1__FOR_BaseUrl_http://google.com:81/
_PollyAttempt 重试次数:2__FOR_BaseUrl_http://google.com:81/
_PollyAttempt 重试次数:3__FOR_BaseUrl_http://google.com:81/
超时拒绝异常内部
(注意它会抛出)这是可以的。因为Polly在超时后会抛出。
但是如果我将http://google.com:81/
改成一个"服务器内部错误"的URL:(返回500)
https://run.mocky.io/v3/9f1b4c18-2cf0-4303-9136-bb67d54d0148
那么它就不会抛出而是继续执行:
_PollyAttempt 重试次数:1__FOR_BaseUrl_https://run.mocky.io/v3/9f1b4c18-2cf0-4303-9136-bb67d54d0148
_PollyAttempt 重试次数:2__FOR_BaseUrl_https://run.mocky.io/v3/9f1b4c18-2cf0-4303-9136-bb67d54d0148
_PollyAttempt 重试次数:3__FOR_BaseUrl_https://run.mocky.io/v3/9f1b4c18-2cf0-4303-9136-bb67d54d0148
工作后
(注意末尾的 "after work")
问题:
为什么 Polly 在超时时会抛出异常,但在其他情况下不会呢? 我明确地写了:.OrResult(msg => httpStatusCodesWorthRetrying.Contains(msg.StatusCode))
其中 500 是其中之一。
OrResult
部分是不必要的。HandleTransientFailure
覆盖 408 和 5xx 状态码。 - Peter Csala