All Genesis.* projects are formalizations of small pieces of functionality I find myself copying from project to project. Some are small to the point of triviality, but are time-savers nonetheless. They have a particular focus on performance with respect to mobile development, but are certainly applicable outside this domain.
Genesis.RetryWithBackoff adds a
RetryWithBackoff extension method to observables (based on this work by @niik). As the name suggests, the
RetryWithBackoff method makes it simple to retry a failing observable with a variable delay between retries.
Genesis.RetryWithBackoff is delivered as a netstandard 1.0 binary.
When using Rx to model asynchrony, it's often desirable to retry a pipeline when it fails. But if that pipeline represents, for example, a web service invocation, we usually want to delay before the retry. Moreover, we want to tailor that delay according to the number of times we've retried. The built-in
Delay operators are not sufficient to achieve this.
The easiest way to get Genesis.RetryWithBackoff is via NuGet:
Genesis.RetryWithBackoff adds a single
RetryWithBackoff extension method to your observable sequences. It's defined in the
System.Reactive.Linq namespace, so you'll generally have access to it if you're already using LINQ to Rx.
Here are some examples:
// retry any number of times, backing off exponentially to a maximum of 3 minutes someObservable .RetryWithBackoff(); // retry up to 3 times, backing off exponentially someObservable .RetryWithBackoff(retryCount: 3); // retry up to 3 times, with 3 seconds between each retry someObservable .RetryWithBackoff( retryCount: 3, strategy: n => TimeSpan.FromSeconds(3)); // retry up to 10 times, but only for InvalidOperationExceptions someObservable .RetryWithBackoff( retryCount: 10, retryOnError: ex => ex is InvalidOperationException); // retry with a custom scheduler (useful for tests) someObservable RetryWithBackoff(scheduler: s);