Posts

Showing posts from October, 2015

Retry algorithm

Retry with exponential back-off Update on July 24, 2016: I just discovered Polly , which does this and more a lot better. I think this is an useful class so I'm just going to leave it here. (I'm annoyed by the duplication between Retry and RetryAsync but I haven't been able to remove it.) public interface RetryPolicy { T Retry<T>(Func<T> func); void Retry(Action action); Task<T> RetryAsync<T>(Func<Task<T>> func); Task RetryAsync(Func<Task> action); } public class RetryPolicyWithExponentialDelay : RetryPolicy { // ReSharper disable once InconsistentNaming public Func<double> GetRandom = () => RND.NextDouble(); // ReSharper disable once InconsistentNaming public Action<int> Sleep = timeout => Thread.Sleep(timeout); public RetryPolicyWithExponentialDelay(int maxCount, TimeSpan initialDelay, TimeSpan maxDelay) { this.maxCount = maxCount; this.i