Skip to content

Instantly share code, notes, and snippets.

@maiconcp
Last active March 31, 2019 01:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maiconcp/79a0dbe47731db38ce3db1943884d6f4 to your computer and use it in GitHub Desktop.
Save maiconcp/79a0dbe47731db38ce3db1943884d6f4 to your computer and use it in GitHub Desktop.
// 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