Edition 0 Updated to asp. Net core 0



Yüklə 11,82 Mb.
Pdf görüntüsü
səhifə262/288
tarix12.07.2023
ölçüsü11,82 Mb.
#136458
1   ...   258   259   260   261   262   263   264   265   ...   288
NET-Microservices-Architecture-for-Containerized-NET-Applications

AddPolicyHandler()
method is what adds policies to the 
HttpClient
objects you’ll use. In this 
case, it’s adding a Polly’s policy for Http Retries with exponential backoff.
To have a more modular approach, the Http Retry Policy can be defined in a separate method within 
the 
Program.cs
file, as shown in the following code: 
static
IAsyncPolicy 
GetRetryPolicy
() 

return
HttpPolicyExtensions 
.
HandleTransientHttpError
() 
.
OrResult
(msg => msg.
StatusCode
== System.
Net
.
HttpStatusCode
.
NotFound

.
WaitAndRetryAsync
(
6
, retryAttempt => TimeSpan.
FromSeconds
(Math.
Pow
(
2

retryAttempt))); 

With Polly, you can define a Retry policy with the number of retries, the exponential backoff 
configuration, and the actions to take when there’s an HTTP exception, such as logging the error. In 
this case, the policy is configured to try six times with an exponential retry, starting at two seconds. 
Add a jitter strategy to the retry policy 
A regular Retry policy can affect your system in cases of high concurrency and scalability and under 
high contention. To overcome peaks of similar retries coming from many clients in partial outages, a 
good workaround is to add a jitter strategy to the retry algorithm/policy. This strategy can improve 
the overall performance of the end-to-end system. As recommended in 
Polly: Retry with Jitter
, a good 
jitter strategy can be implemented by smooth and evenly distributed retry intervals applied with a 
well-controlled median initial retry delay on an exponential backoff. This approach helps to spread out 
the spikes when the issue arises. The principle is illustrated by the following example: 
var
delay = Backoff.
DecorrelatedJitterBackoffV2
(medianFirstRetryDelay: 
TimeSpan.
FromSeconds
(
1
), retryCount: 
5
); 
var
retryPolicy = Policy 
.
Handle
() 
.
WaitAndRetryAsync
(delay); 


307 
CHAPTER 7 | Implement resilient applications 

Yüklə 11,82 Mb.

Dostları ilə paylaş:
1   ...   258   259   260   261   262   263   264   265   ...   288




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin