In the book IntroToRx the author suggest to write a \"smart\" retry for I/O which retry an I/O request, like a network request, after a period of time.
Here is the e
Based on Markus' answer I wrote the following:
public static class ObservableExtensions
{
private static IObservable BackOffAndRetry(
this IObservable source,
Func strategy,
Func retryOnError,
int attempt)
{
return Observable
.Defer(() =>
{
var delay = attempt == 0 ? TimeSpan.Zero : strategy(attempt);
var s = delay == TimeSpan.Zero ? source : source.DelaySubscription(delay);
return s
.Catch(e =>
{
if (retryOnError(attempt, e))
{
return source.BackOffAndRetry(strategy, retryOnError, attempt + 1);
}
return Observable.Throw(e);
});
});
}
public static IObservable BackOffAndRetry(
this IObservable source,
Func strategy,
Func retryOnError)
{
return source.BackOffAndRetry(strategy, retryOnError, 0);
}
}
I like it more because
attempts but uses recursion.retries but passes the number of attempts to retryOnError