RetryPolicyStateWithSleep
using Polly.Utilities;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Polly.Retry
{
internal class RetryPolicyStateWithSleep : IRetryPolicyState
{
private readonly Action<Exception, TimeSpan, Context> _onRetry;
private readonly Context _context;
private readonly IEnumerator<TimeSpan> _sleepDurationsEnumerator;
public async Task<bool> CanRetryAsync(Exception ex, CancellationToken cancellationToken, bool continueOnCapturedContext)
{
if (!_sleepDurationsEnumerator.MoveNext())
return false;
TimeSpan current = _sleepDurationsEnumerator.Current;
_onRetry(ex, current, _context);
await SystemClock.SleepAsync(current, cancellationToken).ConfigureAwait(continueOnCapturedContext);
return true;
}
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)
: this(sleepDurations, delegate(Exception exception, TimeSpan span, Context context) {
onRetry(exception, span);
}, null)
{
}
public bool CanRetry(Exception ex)
{
if (!_sleepDurationsEnumerator.MoveNext())
return false;
TimeSpan current = _sleepDurationsEnumerator.Current;
_onRetry(ex, current, _context);
SystemClock.Sleep(current);
return true;
}
}
}