RetryPolicy<T> Class
- Namespace
- Kampute.Resilience
- Assembly
- Kampute.Resilience.dll
Definition
T: a strategy that decides whether and when to retry, the exceptions and results that are retried, and callbacks for each retry.public sealed class RetryPolicy<T>- Inheritance
- object
- RetryPolicy<T>
Type Parameters
T- The type returned by the operations the policy runs.
Remarks
Properties
Strategy
public IRetryStrategy Strategy { get; }Property Value
- IRetryStrategy
- The IRetryStrategy that decides whether and when to retry.
Methods
Execute(Func<CancellationToken, T>, CancellationToken)
public T Execute(Func<CancellationToken, T> operation, CancellationToken cancellationToken = default)Parameters
operationFunc<CancellationToken, T>- The operation to run. It receives
cancellationToken. cancellationTokenCancellationToken optional- A token for canceling the operation and the waits between attempts (optional).
Returns
- T
- The first result that is not retried, or the last result when the strategy allows no more retries.
Exceptions
- ArgumentNullException
- Thrown if
operationisnull. - OperationCanceledException
- Thrown if
cancellationTokenis canceled while waiting before a retry.
Remarks
Execute<TState>(TState, Func<TState, CancellationToken, T>, CancellationToken)
public T Execute<TState>(TState state, Func<TState, CancellationToken, T> operation, CancellationToken cancellationToken = default)Type Parameters
TState- The type of the state passed to the operation.
Parameters
stateTState- The state passed to every attempt of the operation.
operationFunc<TState, CancellationToken, T>- The operation to run. It receives
stateandcancellationToken. cancellationTokenCancellationToken optional- A token for canceling the operation and the waits between attempts (optional).
Returns
- T
- The first result that is not retried, or the last result when the strategy allows no more retries.
Exceptions
- ArgumentNullException
- Thrown if
operationisnull. - OperationCanceledException
- Thrown if
cancellationTokenis canceled while waiting before a retry.
Remarks
The calling thread is blocked during the waits between attempts. When an exception is not retried, or the strategy allows no more retries, the last exception is rethrown with its original stack trace.
Pass the values that the operation needs as state and use an operation that captures no variables, such as a static lambda; the compiler then reuses one delegate instance, so the call allocates no closure. Use a tuple to pass several values.
ExecuteAsync(Func<CancellationToken, Task<T>>, CancellationToken)
public Task<T> ExecuteAsync(Func<CancellationToken, Task<T>> operation, CancellationToken cancellationToken = default)Parameters
operationFunc<CancellationToken, Task<T>>- The operation to run. It receives
cancellationToken. cancellationTokenCancellationToken optional- A token for canceling the operation and the waits between attempts (optional).
Returns
- Task<T>
- A task that resolves to the first result that is not retried, or the last result when the strategy allows no more retries.
Exceptions
- ArgumentNullException
- Thrown, before a task is returned, if
operationisnull. - OperationCanceledException
- Thrown if
cancellationTokenis canceled while waiting before a retry.
Remarks
ExecuteAsync<TState>(TState, Func<TState, CancellationToken, Task<T>>, CancellationToken)
public Task<T> ExecuteAsync<TState>(TState state, Func<TState, CancellationToken, Task<T>> operation, CancellationToken cancellationToken = default)Type Parameters
TState- The type of the state passed to the operation.
Parameters
stateTState- The state passed to every attempt of the operation.
operationFunc<TState, CancellationToken, Task<T>>- The operation to run. It receives
stateandcancellationToken. cancellationTokenCancellationToken optional- A token for canceling the operation and the waits between attempts (optional).
Returns
- Task<T>
- A task that resolves to the first result that is not retried, or the last result when the strategy allows no more retries.
Exceptions
- ArgumentNullException
- Thrown, before a task is returned, if
operationisnull. - OperationCanceledException
- Thrown if
cancellationTokenis canceled while waiting before a retry.
Remarks
When an exception is not retried, or the strategy allows no more retries, the last exception is rethrown with its original stack trace.
Pass the values that the operation needs as state and use an operation that captures no variables, such as a static lambda; the compiler then reuses one delegate instance, so the call allocates no closure. Use a tuple to pass several values.
OnDiscarded(Action<T>)
public RetryPolicy<T> OnDiscarded(Action<T> handler)Parameters
handlerAction<T>- The handler, which receives the discarded result.
Returns
- RetryPolicy<T>
- A new policy that invokes the discard handlers of this policy and then
handler.
Exceptions
- ArgumentNullException
- Thrown if
handlerisnull.
Remarks
A retried result is discarded once: after the retry handlers and before the wait, or when a result classifier, delay override, retry handler, or wait fails. A result that is not retried, and the last result returned when the strategy allows no more retries, are not discarded and remain the caller's.
If a handler throws, later handlers do not run, and its exception propagates in place of any earlier callback or wait exception.
OnRetry(Action<RetryContext<T>>)
public RetryPolicy<T> OnRetry(Action<RetryContext<T>> handler)Parameters
handlerAction<RetryContext<T>>- The handler, which receives the failed attempt and the delay that execution waits before the retry.
Returns
- RetryPolicy<T>
- A new policy that invokes the handlers of this policy and then
handler.
Exceptions
- ArgumentNullException
- Thrown if
handlerisnull.
Remarks
OverrideDelay(Func<RetryContext<T>, TimeSpan>)
public RetryPolicy<T> OverrideDelay(Func<RetryContext<T>, TimeSpan> delay)Parameters
delayFunc<RetryContext<T>, TimeSpan>- A function that receives the failed attempt, whose RetryContext<T>.Delay is the delay that the strategy proposes, and returns the nonnegative delay to wait. Return RetryContext<T>.Delay to keep the proposed delay.
Returns
- RetryPolicy<T>
- A new policy whose delays
delayselects. It replaces any delay override of this policy.
Exceptions
- ArgumentNullException
- Thrown if
delayisnull.
Remarks
The override runs only after the strategy allows a retry, and its delay replaces the proposed delay after all strategy modifiers, so it can exceed a delay cap or the time left under an elapsed-time limit. Enforce any required deadline in the override or with the caller's cancellation token.
A negative delay throws ArgumentOutOfRangeException from the execution, without a retry. A delay longer than int.MaxValue milliseconds stops retrying, as when the strategy allows no more retries.
RetryOn(Func<Exception, bool>)
public RetryPolicy<T> RetryOn(Func<Exception, bool> predicate)Parameters
Returns
- RetryPolicy<T>
- A new policy that retries the exceptions this policy retries and the exceptions that
predicateselects.
Exceptions
- ArgumentNullException
- Thrown if
predicateisnull.
Remarks
RetryOn call limits retries to the exceptions it selects; later calls add to them. If predicate throws, the operation's exception propagates without a retry.RetryOn<TException>()
public RetryPolicy<T> RetryOn<TException>()
where TException : ExceptionType Parameters
TException- The type of the exceptions to retry, including derived types.
Returns
- RetryPolicy<T>
- A new policy that retries the exceptions this policy retries and the exceptions of type
TException.
Remarks
RetryOn call limits retries to the exceptions it selects; later calls add to them.RetryOn<TException>(Func<TException, bool>)
public RetryPolicy<T> RetryOn<TException>(Func<TException, bool> predicate)
where TException : ExceptionType Parameters
TException- The type of the exceptions to retry, including derived types.
Parameters
predicateFunc<TException, bool>- A function that returns
truefor the exceptions of typeTExceptionto retry.
Returns
- RetryPolicy<T>
- A new policy that retries the exceptions this policy retries and the exceptions that
predicateselects.
Exceptions
- ArgumentNullException
- Thrown if
predicateisnull.
Remarks
RetryOn call limits retries to the exceptions it selects; later calls add to them. If predicate throws, the operation's exception propagates without a retry.RetryOnResult(Func<T, bool>)
public RetryPolicy<T> RetryOnResult(Func<T, bool> predicate)Parameters
Returns
- RetryPolicy<T>
- A new policy that retries the results this policy retries and the results that
predicateselects.
Exceptions
- ArgumentNullException
- Thrown if
predicateisnull.

Create this policy with RetryPolicy.RetryOnResult<T>(Func<T, bool>) or RetryOnResult<T>(this IRetryStrategy, Func<T, bool>). Like RetryPolicy, it is immutable: each configuration method returns a new policy, and a policy can be shared by concurrent executions.
When the strategy allows no more retries after a retried result, execution returns that result. Results that execution does not return are passed to the handlers registered with OnDiscarded(Action<T>); the result that execution returns remains the caller's.
Callbacks run on the execution path. If an exception classifier throws, the operation's exception propagates as if it was not retried; an exception from a result classifier, delay override, retry handler, or discard handler propagates in place of the operation's outcome, and the operation is not retried.