RetryStateRetryWithCount<TResult>
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Polly.Retry
{
internal class RetryStateRetryWithCount<TResult> : IRetryPolicyState<TResult>
{
private readonly Func<DelegateResult<TResult>, int, Context, Task> _onRetryAsync;
private int _errorCount;
private readonly int _retryCount;
private readonly Action<DelegateResult<TResult>, int, Context> _onRetry;
private readonly Context _context;
public RetryStateRetryWithCount(int retryCount, Func<DelegateResult<TResult>, int, Context, Task> onRetryAsync, Context context)
{
_retryCount = retryCount;
_onRetryAsync = onRetryAsync;
_context = context;
}
public async Task<bool> CanRetryAsync(DelegateResult<TResult> delegateResult, CancellationToken ct, bool continueOnCapturedContext)
{
this._errorCount++;
bool shouldRetry = this._errorCount <= this._retryCount;
if (shouldRetry)
await this._onRetryAsync(delegateResult, this._errorCount, this._context).ConfigureAwait(continueOnCapturedContext);
return shouldRetry;
}
public RetryStateRetryWithCount(int retryCount, Action<DelegateResult<TResult>, int, Context> onRetry, Context context)
{
_retryCount = retryCount;
_onRetry = onRetry;
_context = context;
}
public bool CanRetry(DelegateResult<TResult> delegateResult, CancellationToken cancellationToken)
{
_errorCount++;
bool num = _errorCount <= _retryCount;
if (num)
_onRetry(delegateResult, _errorCount, _context);
return num;
}
}
}