RetryPolicyStateWithCount<TResult>
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Polly.Retry
{
internal class RetryPolicyStateWithCount<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 RetryPolicyStateWithCount(int retryCount, Func<DelegateResult<TResult>, int, Context, Task> onRetryAsync, Context context)
{
_retryCount = retryCount;
_onRetryAsync = onRetryAsync;
_context = context;
}
public RetryPolicyStateWithCount(int retryCount, Func<DelegateResult<TResult>, int, Task> onRetryAsync)
: this(retryCount, (Func<DelegateResult<TResult>, int, Context, Task>)((DelegateResult<TResult> delegateResult, int i, Context context) => onRetryAsync(delegateResult, i)), Context.Empty)
{
}
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 RetryPolicyStateWithCount(int retryCount, Action<DelegateResult<TResult>, int, Context> onRetry, Context context)
{
_retryCount = retryCount;
_onRetry = onRetry;
_context = context;
}
public RetryPolicyStateWithCount(int retryCount, Action<DelegateResult<TResult>, int> onRetry)
: this(retryCount, (Action<DelegateResult<TResult>, int, Context>)delegate(DelegateResult<TResult> delegateResult, int i, Context context) {
onRetry(delegateResult, i);
}, Context.Empty)
{
}
public bool CanRetry(DelegateResult<TResult> delegateResult)
{
_errorCount++;
bool num = _errorCount <= _retryCount;
if (num)
_onRetry(delegateResult, _errorCount, _context);
return num;
}
}
}