Class LinearRetryPolicy
Implements a simple IRetryPolicy that retries an operation at a fixed interval for a specified maximum number of times.
Implements
Inherited Members
Namespace: Neon.Retry
Assembly: Neon.Common.dll
Syntax
public class LinearRetryPolicy : RetryPolicyBase, IRetryPolicy
Remarks
You can enable transient error logging by passing a non-empty logCategory name to the constructor. This creates an embedded ILogger using that name and any retried transient errors will then be logged as warnings including [transient-retry] in the message.
note
Only the retried errors will be logged. The final exception thrown after all retries fail will not be logged because it's assumed that these will be caught and handled upstack by application code.
Choose a category name that can be used to easily identify the affected component. For example, yugabyte:my-cluster to identify a specific YugaBYte database cluster.
Constructors
LinearRetryPolicy(Func<Exception, bool>, int, TimeSpan?, TimeSpan?, string)
Constructs the retry policy with a specific transitent detection function.
Declaration
public LinearRetryPolicy(Func<Exception, bool> transientDetector = null, int maxAttempts = -1, TimeSpan? retryInterval = null, TimeSpan? timeout = null, string categoryName = "transient-errors")
Parameters
Type | Name | Description |
---|---|---|
Func<Exception, bool> | transientDetector | Optionally specifies the function that determines whether an exception is transient
(see TransientDetector). You can pass |
int | maxAttempts | Optionally specifies the maximum number of times an action should be retried (defaults to 5). |
TimeSpan? | retryInterval | Optionally specifies time interval between retry attempts (defaults to 1 second). |
TimeSpan? | timeout | Optionally specifies the maximum time the operation will be retried (defaults to unconstrained) |
string | categoryName | Optionally customizes the transient error logging source category name (defaults to DefaultCategoryName).
You can disable transient error logging by passing |
Remarks
The maxAttempts
parameter defaults to -1 indicating that the
operation should be attempted up to 5 times, unless a Timeout is
specified. In this case, maxAttempts
will be ignored and the timeout
will be honored.
When maxAttempts
is greater than or equal to zero and Timeout
is passed, then both maxAttempts
and Timeout will be honored,
with retries stopping when either are exceeded.
LinearRetryPolicy(Type, int, TimeSpan?, TimeSpan?, string)
Constructs the retry policy to handle a specific exception type as transient.
Declaration
public LinearRetryPolicy(Type exceptionType, int maxAttempts = -1, TimeSpan? retryInterval = null, TimeSpan? timeout = null, string categoryName = null)
Parameters
Type | Name | Description |
---|---|---|
Type | exceptionType | Specifies the exception type to be considered to be transient. |
int | maxAttempts | Optionally specifies the maximum number of times an action should be retried (defaults to 5). |
TimeSpan? | retryInterval | Optionally specifies the time interval between retry attempts (defaults to 1 second). |
TimeSpan? | timeout | Optionally specifies the maximum time the operation will be retried (defaults to unconstrained) |
string | categoryName | Optionally enables transient error logging by identifying the source category name (defaults to |
Remarks
The maxAttempts
parameter defaults to -1 indicating that the
operation should be attempted up to 5 times, unless a Timeout is
specified. In this case, maxAttempts
will be ignored and the timeout
will be honored.
When maxAttempts
is greater than or equal to zero and Timeout
is passed, then both maxAttempts
and Timeout will be honored,
with retries stopping when either are exceeded.
LinearRetryPolicy(Type[], int, TimeSpan?, TimeSpan?, string)
Constructs the retry policy to handle a multiple exception types as transient.
Declaration
public LinearRetryPolicy(Type[] exceptionTypes, int maxAttempts = -1, TimeSpan? retryInterval = null, TimeSpan? timeout = null, string categoryName = null)
Parameters
Type | Name | Description |
---|---|---|
Type[] | exceptionTypes | Specifies the exception types to be considered to be transient. |
int | maxAttempts | Optionally specifies the maximum number of times an action should be retried (defaults to 5). |
TimeSpan? | retryInterval | Optionally specifies the time interval between retry attempts (defaults to 1 second). |
TimeSpan? | timeout | Optionally specifies the maximum time the operation will be retried (defaults to unconstrained) |
string | categoryName | Optionally enables transient error logging by identifying the source category name (defaults to |
Remarks
The maxAttempts
parameter defaults to -1 indicating that the
operation should be attempted up to 5 times, unless a Timeout is
specified. In this case, maxAttempts
will be ignored and the timeout
will be honored.
When maxAttempts
is greater than or equal to zero and Timeout
is passed, then both maxAttempts
and Timeout will be honored,
with retries stopping when either are exceeded.
Properties
MaxAttempts
Returns the maximum number of times the action should be attempted.
Declaration
public int MaxAttempts { get; }
Property Value
Type | Description |
---|---|
int |
RetryInterval
Returns the fixed interval between action retry attempts.
Declaration
public TimeSpan RetryInterval { get; }
Property Value
Type | Description |
---|---|
TimeSpan |
Methods
Clone(Func<Exception, bool>)
Returns a copy of the retry policy.
Declaration
public override 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. |
Overrides
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 override 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. |
Overrides
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 override 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 |
Overrides
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 override 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. |
Overrides
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 override 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. |