This article is a mirror article of machine translation, please click here to jump to the original article.

View: 22130|Reply: 3

[Frame] . .NET resiliency and transient failure handling Polly retry strategy

[Copy link]
Posted on 5/28/2019 11:54:53 PM | | | |
This post was last edited by Kongcida on 2019-5-29 11:16

preface
In a project, there will be problems with project operation due to uncontrollable reasons. For example, a server failure, a service crash, network outage, etc. Encountering some retryable errors, we can use Polly —— . .NET Elastic and Transient Fault Handling Libraries. Project official website address (The hyperlink login is visible.)。 Today this article will document Polly's bad retry strategy.


Environment & Tools


>VS 2017
>Windows 10 system


Text


1: Create a new console application.

2: Quote Polly. Available for download in the NuGet package

3: Paste the code



4: F6 generates. Then find the generated .exe file under the debug folder – > double-click to run.



You can see the output message: Retried three times.

When F5 is debugging, you can also see that this method has been done three times.


Polly's retry strategy is explained in detail

1: Exception type


In this code, the :handle contains the exception type to be retried. That is, when the executed code produces a specific exception, the retry function is executed. Otherwise, no retry will be performed. Because some exceptions do not require a retry.

Of course, we can fill in a number of specific exception types here to retry.



The DivideByZeroException here represents an exception that attempts to divide by zero.
SqlException indicates an exception that is raised when Sql Server returns an error or warning. For example, database connection failure, SQL execution error, etc.
SmtpException indicates an exception raised by the mail service. For example, the sending failed, the network connection failed, and the email account information was abnormal.

For specific exception types, please refer to Microsoft's official documentation:



2: Retry type

1) The first type of retry is a direct given number of retries. The following code is only retried 3 times.

We can call the relevant actions performed on each retry when retrying. The following code is another commission later. When performing a retry, the number of retries and the reason for the exception are printed.



We can also keep the code retrying until it succeeds.



2) The second type of retry is to retry at intervals. The following code means to retry every 2 seconds, 7 seconds, or 10 seconds. Retry to 10 seconds. If the execution is not successful, end the retry.



We can also try again at intervals until the retry is successful. This requires the calculation of a function.



The above code refers to the power retry of indirect 2. For example, if the execution fails, the first time will be retried at intervals of 2 seconds, the second time will be retried at intervals of 4 seconds, and the third time will be retried at intervals of 8 seconds......... And so on. Until execution is successful.



Epilogue


Note: The exception types in the above code Handle should be filled in according to the actual situation in your actual project. Not all exceptions require a retry.

The exception type is the base class for all exceptions. It is best not to fill this in the handle, otherwise, it will have very serious consequences.














Previous:. Net integration PayPal IPN return value has always been INVALID
Next:C# Usage of yield keywords
Posted on 5/29/2019 9:17:54 AM |
I implemented the retry mechanism myself in AOP, and in the end, I found that the Polly library can meet my needs, but my function has been implemented, and I refer to a third-party library, which may have some useless code in it, which cannot optimize my system, so it is not useful for the time being.

Attached is my code:



When I have time, I'll also look into Polly
Posted on 5/29/2019 9:22:01 AM |
Xiao Zhaozha Posted on 2019-5-29 09:17
I implemented the retry mechanism myself in aop, and in the end, I found that the Polly library could meet my needs, but my function has been implemented, and the introduction ...

There is nothing wrong with doing small needs like this yourself
And you should wait for the next time to retry
Posted on 5/29/2019 9:29:22 AM |
winds posted on 2019-5-29 09:22
There is nothing wrong with doing small needs like this yourself
And should you wait for the next time to try again?

No, the exception must be caused by concurrency, data insertion, update, and deletion conflicts, but there must be one that will execute successfully, I ....... The omitted code wraps the transaction, and the exception will be automatically rolled back. On the second retry, the conflict is gone, so I didn't set a wait time.
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com