<PackageReference Include="Polly" Version="2.1.0" />

RetryPolicyStateWithSleep

using Polly.Utilities; using System; using System.Collections.Generic; namespace Polly.Retry { internal class RetryPolicyStateWithSleep : IRetryPolicyState { private readonly Action<Exception, TimeSpan, Context> _onRetry; private readonly Context _context; private readonly IEnumerator<TimeSpan> _sleepDurationsEnumerator; public RetryPolicyStateWithSleep(IEnumerable<TimeSpan> sleepDurations, Action<Exception, TimeSpan, Context> onRetry, Context context) { _onRetry = onRetry; _context = context; _sleepDurationsEnumerator = sleepDurations.GetEnumerator(); } public RetryPolicyStateWithSleep(IEnumerable<TimeSpan> sleepDurations, Action<Exception, TimeSpan> onRetry) { Action<Exception, TimeSpan, Context> onRetry2 = delegate(Exception exception, TimeSpan span, Context context) { onRetry(exception, span); }; this..ctor(sleepDurations, onRetry2, null); } public bool CanRetry(Exception ex) { if (!_sleepDurationsEnumerator.MoveNext()) return false; TimeSpan current = _sleepDurationsEnumerator.Current; _onRetry(ex, current, _context); SystemClock.Sleep(current); return true; } } }