Skip to content

Latest commit

 

History

History
61 lines (40 loc) · 2.69 KB

README.md

File metadata and controls

61 lines (40 loc) · 2.69 KB

Logo

Genesis.RetryWithBackoff

Build status

What?

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.

Why?

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 Retry and Delay operators are not sufficient to achieve this.

Where?

The easiest way to get Genesis.RetryWithBackoff is via NuGet:

Install-Package Genesis.RetryWithBackoff

How?

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);

Who?

Genesis.RetryWithBackoff is created and maintained by Kent Boogaart. However, it is based on original code is by @niik. Issues and pull requests are welcome.