Class RetryPolicyBase
Base class for used to help implement a IRetryPolicy.
Implements
Namespace: Neon.Retry
Assembly: Neon.Common.dll
Syntax
public abstract class RetryPolicyBase : IRetryPolicy
Constructors
RetryPolicyBase(string, TimeSpan?)
Constructor.
Declaration
public RetryPolicyBase(string categoryName = "transient-errors", TimeSpan? timeout = null)
Parameters
Type | Name | Description |
---|---|---|
string | categoryName | Optionally customizes the transient error logging source category name (defaults to DefaultCategoryName).
You can disable transient error logging by passing |
TimeSpan? | timeout | Optionally specifies the maximum time the operation will be retried (defaults to unconstrained). |
Fields
DefaultCategoryName
Specifies the default default category name for logging transient exceptions.
Declaration
protected const string DefaultCategoryName = "transient-errors"
Field Value
Type | Description |
---|---|
string |
Properties
CategoryName
Returns the associated log source category name (or null)
.
Declaration
protected string CategoryName { get; }
Property Value
Type | Description |
---|---|
string |
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, DateTime)
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, DateTime sysDeadline)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | delay | The requested delay. |
DateTime | sysDeadline | The retry deadline (SYS) computed by SysDeadline(). |
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 (or there were no handlers).
Declaration
protected void LogTransient(Exception e)
Parameters
Type | Name | Description |
---|---|---|
Exception | e | The transient exception. |
SysDeadline()
Computes the time (SYS) after which the operation should not be retried.
Declaration
protected DateTime SysDeadline()
Returns
Type | Description |
---|---|
DateTime |
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> |