只需对Pollys超时/重试策略以及处理并发HTTP请求时的工作方式。
从阅读/我自己的理解,将将超时和重试策略应用于每个单独的HTTP请求,因此,如果我们有5个HTTP请求,每个请求都将具有自己的超时和重试策略,因此,从下面的代码中,每个HTTP请求都会超时5秒后重试4次。
public override void Configure(IFunctionsHostBuilder builder)
{
var timeout = Policy.TimeoutAsync<HttpResponseMessage>(TimeSpan.FromSeconds(5));
builder.Services
.AddHttpClient("PointsbetClient")
.AddPolicyHandler(GetRetryPolicy())
.AddPolicyHandler(timeout);
}
private static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
{
return HttpPolicyExtensions
.HandleTransientHttpError()
.Or<TimeoutRejectedException>()
.WaitAndRetryAsync(Backoff.DecorrelatedJitterBackoffV2(
medianFirstRetryDelay: TimeSpan.FromMilliseconds(500),
retryCount: 4));
}
现在说我有一个1000 http请求,我需要对其进行getAsync()调用,以便我可以刮擦他们的数据并出于性能目的,我通过使用等待task.whenall(tasks)并同时进行这些调用;
。由于1000在一次使用 Semaphoreslim
类并将MaxParallelleRequests限制为100的要求时太多
。它仍然适用于每个单独的1000个请求中的每个请求,还是将包含100条请求的1个任务视为一个超时/重试策略?从我的理解中,它仍然可以治疗和应用政策对每个单独的HTTP请求,我一直在搜索,但找不到确认。
Just have a question about Pollys timeout/retry policy and how it works when handling concurrent http requests.
From reading/my own understanding the timeout and retry policy will be applied to each individual http request, so if we have 5 http requests, each of them would have there own timeout and retry policy, so from the code below each http request would timeout after 5 seconds and retry a total of 4 times.
public override void Configure(IFunctionsHostBuilder builder)
{
var timeout = Policy.TimeoutAsync<HttpResponseMessage>(TimeSpan.FromSeconds(5));
builder.Services
.AddHttpClient("PointsbetClient")
.AddPolicyHandler(GetRetryPolicy())
.AddPolicyHandler(timeout);
}
private static IAsyncPolicy<HttpResponseMessage> GetRetryPolicy()
{
return HttpPolicyExtensions
.HandleTransientHttpError()
.Or<TimeoutRejectedException>()
.WaitAndRetryAsync(Backoff.DecorrelatedJitterBackoffV2(
medianFirstRetryDelay: TimeSpan.FromMilliseconds(500),
retryCount: 4));
}
Now say I have a 1000 http requests to which I need to make GetAsync() calls to so I can scrape their data and for performance purposes, I am making these calls concurrently, by using await Task.WhenAll(tasks);
. Since 1000 is way too many requests to hit at the one time I am using SemaphoreSlim
class and limiting the MaxParallelRequests to 100.
How would the Polly retry and timeout policy apply now? does it still apply to each of those individual 1000 requests or will it treat 1 task containing 100 requests as a single timeout/retry policy? From my understanding it would still treat and apply policy to each of the individual http requests, I've been searching but can't find confirmation on this.
发布评论
评论(1)
简短的答案是,他们被分开对待。
为了了解系统的工作原理,我们必须在引擎盖下看。让我们从
addpolicyhandler
开始我们的旅程。免责声明:为了简洁起见
。 /aspnetcore/blob/A450CB69B5E4549F5515CDB057A687771F56CEFD7/SRC/HTTPCLEINTFACTORY/POLLY/POLLY/SRC/DEPRIANDENCHIN/depporendentynipportion/pollyhtttppclientbuilderextensions.csssentions.css.cs.cs.cs.c.cs.c.cs.c.c.c.cperternertly> ClientbuildErxensions 类,并为
ihttpclientbuilder
。如您所见,它对它无能为力,只是注册另一个
httpmessagehandler
进入链条。现在,让我们看看这个特殊的处理
程序看起来像
policyhttpmessagehandler
。正如您所看到的那样,
sendcoreasync
的策略,因此,魔术发生在哪里。 ? Let's jump to the documentation comment
要了解重试与断路器有何不同之处,让我们查看其
引擎
s'实现
签名“ 174CC53E17BF02DA5E1F2C0D74DFFB4F23A99C0/SRC/POLLY/CRUCIENBREAKER/CRUCIENBREAKER/CRUCIENBREAKERENGINE.CS#l9“ rel =“ nofollow noreferrer”>“ nofollow noreferrer”> croutlerBreakerEngrengine
您在这里要发现的是 基类存储状态信息。其派生类之一是在不同的策略执行之间,
我希望这会澄清一切。
The short answer is yes they are treated separately.
In order to understand how the system works we have to look under the hood. Let's start our journey at the
AddPolicyHandler
.Disclaimer: I've slightly edited the code snippets for the sake of brevity.
This method is defined inside the
PollyHttpClientBuilderExtensions
class and it provides extension methods for theIHttpClientBuilder
.As you can see it does nothing else just registers yet another
HttpMessageHandler
into the chain.Now, let's see how does this special handler look like
This method is defined inside the
PolicyHttpMessageHandler
.As you can see nothing extraordinary happens here
SendCoreAsync
So, where does the magic happen? Let's jump to the documentation comment of this class
To understand how does Retry differ from Circuit Breaker lets look at their
Engine
s'Implementation
signatureRetryEngine
CircuitBreakerEngine
What you have to spot here is the
ICircuitController
. TheCircuitStateController
base class stores the state information. One of its derived class is shared between the different policy executionsI hope this clarify things.