Skip to content

Instantly share code, notes, and snippets.

@maiconcp

maiconcp/Polly_Retry.cs

Last active Mar 31, 2019
Embed
What would you like to do?
// Retry a specified number of times, using a function to
// calculate the duration to wait between retries based on
// the current retry attempt (allows for exponential backoff)
// In this case will wait for
// 2 ^ 1 = 2 seconds then
// 2 ^ 2 = 4 seconds then
// 2 ^ 3 = 8 seconds then
// 2 ^ 4 = 16 seconds then
// 2 ^ 5 = 32 seconds
Policy
.Handle<SomeException>()
.WaitAndRetry(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))
.Execute(() =>
{
// myService.Post(...)...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment