Polly 不会抛出一些异常? [英] Polly won't throw on some exceptions?

查看:27
本文介绍了Polly 不会抛出一些异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将 Polly 与 .net Core 一起使用.我的 ConfigureServices 是:

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

这是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()}");
        });
}

这些是我想重试的 httpcodes :

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

输出是:

_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/
内部 TimeoutRejectedException

_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

(注意它抛出)这是可以的.因为 Polly 在这个无效的 URL 超时后抛出.

(notice it throws) Which is OK. Because Polly throws after this invalid URL is timeout.

但是如果我将 http://google.com:81/ 更改为内部服务器错误"url : (这个返回500)

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
下班后

_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

(注意下班后")

问题:

为什么 Polly 在超时时抛出,但不在另一个条件下抛出?我明确地写道: .OrResult(msg => httpStatusCodesWorthRetrying.Contains(msg.StatusCode)) 500 就是其中之一.

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.

推荐答案

这是预期的行为.委托调用会导致异常或返回值.当 Polly 重试完成后,它会传播上次的任何结果,无论是异常还是返回值.

This is expected behavior. A delegate invocation results in either an exception or a return value. When the Polly retries are done, then it propagates whatever result was last, whether it is an exception or a return value.

在这种情况下,响应将具有 500 状态代码.

In this case, the response would have a 500 status code.

这篇关于Polly 不会抛出一些异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆