Class RetryPolicy
Base class for used to help implement a IRetryPolicy.
Implements
Namespace: Neon.Retry
Assembly: Neon.Common.dll
Syntax
public abstract class RetryPolicy : IRetryPolicy
Constructors
RetryPolicy(string, TimeSpan?)
Constructor.
Declaration
public RetryPolicy(string categoryName = null, TimeSpan? timeout = null)
Parameters
Type | Name | Description |
---|---|---|
string | categoryName | Optionally enables transient error logging by identifying the source category name (defaults to |
TimeSpan? | timeout | Optionally specifies the maximum time the operation should be retried (defaults to no limit). |
Fields
DefaultMaxAttempts
The default number of times an operation should be attempted for transient exceptions.
Declaration
public const int DefaultMaxAttempts = 5
Field Value
Type | Description |
---|---|
int |
Properties
Timeout
Returns the optional policy timeout. When present, this specifies the maximum time the policy will continue retrying the operation.
Declaration
public TimeSpan? Timeout { get; }
Property Value
Type | Description |
---|---|
TimeSpan? |
Methods
AdjustDelay(TimeSpan)
Adjusts the delay TimeSpan passed to ensure such that delaying the next retry won't exceed the overall retry timeout (if specified).
Declaration
protected TimeSpan AdjustDelay(TimeSpan delay)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | delay | The requested delay. |
Returns
Type | Description |
---|---|
TimeSpan | The adjusted delay. |
Remarks
note
If the result is Zero or negative, the calling retry policy should immediately stop retrying.
Clone(Func<Exception, bool>)
Returns a copy of the retry policy.
Declaration
public abstract IRetryPolicy Clone(Func<Exception, bool> transientDetector = null)
Parameters
Type | Name | Description |
---|---|---|
Func<Exception, bool> | transientDetector | Optionally specifies a replacement transient detector function that will be set in the cloned policy. |
Returns
Type | Description |
---|---|
IRetryPolicy | The policy copy. |
Invoke(Action, CancellationToken)
Retries a synchronous action that returns no result when it throws exceptions due to transient errors. The classification of what is a transient error, the interval between the retries as well as the number of times the operation are retried are determined by the policy implementation.
Declaration
public abstract void Invoke(Action action, CancellationToken cancellationToken = default)
Parameters
Type | Name | Description |
---|---|---|
Action | action | The synchronous action to be performed. |
CancellationToken | cancellationToken | Optionally specifies a cancellation token. |
InvokeAsync(Func<Task>, CancellationToken)
Retries an asynchronous action that returns no result when it throws exceptions due to transient errors. The classification of what is a transient error, the interval between the retries as well as the number of times the operation are retried are determined by the policy implementation.
Declaration
public abstract Task InvokeAsync(Func<Task> action, CancellationToken cancellationToken = default)
Parameters
Type | Name | Description |
---|---|---|
Func<Task> | action | The asynchronous action to be performed. |
CancellationToken | cancellationToken | Optionally specifies a cancellation token. |
Returns
Type | Description |
---|---|
Task |
InvokeAsync<TResult>(Func<Task<TResult>>, CancellationToken)
Retries an asynchronous action that returns TResult when it throws exceptions due to transient errors. he classification of what is a transient error, the interval between the retries as well as the number of times the operation are retried are determined by the policy implementation.
Declaration
public abstract Task<TResult> InvokeAsync<TResult>(Func<Task<TResult>> action, CancellationToken cancellationToken = default)
Parameters
Type | Name | Description |
---|---|---|
Func<Task<TResult>> | action | The asynchronous action to be performed. |
CancellationToken | cancellationToken | Optionally specifies a cancellation token. |
Returns
Type | Description |
---|---|
Task<TResult> | The action result. |
Type Parameters
Name | Description |
---|---|
TResult | The action result type. |
Invoke<TResult>(Func<TResult>, CancellationToken)
Retries a synchronous action that returns a result when it throws exceptions due to transient errors. The classification of what is a transient error, the interval between the retries as well as the number of times the operation are retried are determined by the policy implementation.
Declaration
public abstract TResult Invoke<TResult>(Func<TResult> action, CancellationToken cancellationToken = default)
Parameters
Type | Name | Description |
---|---|---|
Func<TResult> | action | The synchronous action to be performed. |
CancellationToken | cancellationToken | Optionally specifies a cancellation token. |
Returns
Type | Description |
---|---|
TResult | The action result. |
Type Parameters
Name | Description |
---|---|
TResult | The action result type. |
LogTransient(Exception)
Handles logging of transient exceptions by invoking any OnTransient event handlers and then logging the transient exception when none of the handlers indicated that they handled the event.
Declaration
protected void LogTransient(Exception e)
Parameters
Type | Name | Description |
---|---|---|
Exception | e | The transient exception. |
Events
OnTransient
Used to intercept and handle logging for transient exceptions detected by a retry policy. Handlers can set Handled in the argument passed to prevent subsequent handlers from being invoked and also prevent the transient exception from being logged.
When no handlers are added to this event, the default behavior is to log all transient failures.
Declaration
public event Action<RetryTransientArgs> OnTransient
Event Type
Type | Description |
---|---|
Action<RetryTransientArgs> |