Custom retry mechanism based on Polly
Index
- The problem
- One (of many) solution
- Code snippet
- References
The problem
Network connectivity issues, flaky dependencies (APIs, databases, cloud services...) might became a painfull when you work in a production environment. Let's make your life a bit easier with Polly.One (of many) solution
Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner. Polly targets .NET 4.0, .NET 4.5 and .NET Standard 1.1.The following code snippet allow you to define how your retry will look like based on: number of retries (it works in an exponential way 2, 4, 16 seconds...), retry policy (expected result), action to be retried and exception type on which you want to retry.
Polly provides asynchronous methods so your retries will never block your threads.
Disclaimer: I went for an exponential retry but you can define simple elapsed time to retry on.
Code snippet
References
https://github.com/App-vNext/Pollyhttp://www.thepollyproject.org