Table of Contents

Class RetryPolicyBase

Namespace
LinqToDB.Data.RetryPolicy
Assembly
linq2db.dll
public abstract class RetryPolicyBase : IRetryPolicy
Inheritance
RetryPolicyBase
Implements
Derived
Extension Methods

Constructors

RetryPolicyBase(int, TimeSpan, double, double, TimeSpan)

Creates a new instance of RetryPolicyBase.

protected RetryPolicyBase(int maxRetryCount, TimeSpan maxRetryDelay, double randomFactor, double exponentialBase, TimeSpan coefficient)

Parameters

maxRetryCount int

The maximum number of retry attempts.

maxRetryDelay TimeSpan

The maximum delay in milliseconds between retries.

randomFactor double

The maximum random factor.

exponentialBase double

The base for the exponential function used to compute the delay between retries.

coefficient TimeSpan

The coefficient for the exponential function used to compute the delay between retries.

Properties

Coefficient

The coefficient for the exponential function used to compute the delay between retries, must be nonnegative.

public TimeSpan Coefficient { get; }

Property Value

TimeSpan

ExceptionsEncountered

The list of exceptions that caused the operation to be retried so far.

protected virtual List<Exception> ExceptionsEncountered { get; }

Property Value

List<Exception>

ExponentialBase

The base for the exponential function used to compute the delay between retries, must be positive.

public double ExponentialBase { get; }

Property Value

double

MaxRetryCount

The maximum number of retry attempts.

protected virtual int MaxRetryCount { get; }

Property Value

int

MaxRetryDelay

The maximum delay in milliseconds between retries.

protected virtual TimeSpan MaxRetryDelay { get; }

Property Value

TimeSpan

Random

A pseudo-random number generator that can be used to vary the delay between retries.

protected virtual Random Random { get; }

Property Value

Random

RandomFactor

The maximum random factor, must not be lesser than 1.

public double RandomFactor { get; }

Property Value

double

Suspended

Indicates whether the strategy is suspended. The strategy is typically suspending while executing to avoid recursive execution from nested operations.

protected static bool Suspended { get; set; }

Property Value

bool

Methods

Execute(Action)

public virtual void Execute(Action operation)

Parameters

operation Action

ExecuteAsync(Func<CancellationToken, Task>, CancellationToken)

public Task ExecuteAsync(Func<CancellationToken, Task> operation, CancellationToken cancellationToken = default)

Parameters

operation Func<CancellationToken, Task>
cancellationToken CancellationToken

Returns

Task

ExecuteAsync<TResult>(Func<CancellationToken, Task<TResult>>, CancellationToken)

Executes the specified asynchronous operation and returns the result.

public virtual Task<TResult> ExecuteAsync<TResult>(Func<CancellationToken, Task<TResult>> operation, CancellationToken cancellationToken = default)

Parameters

operation Func<CancellationToken, Task<TResult>>

A function that returns a started task of type TResult.

cancellationToken CancellationToken

A cancellation token used to cancel the retry operation, but not operations that are already in flight or that already completed successfully.

Returns

Task<TResult>

A task that will run to completion if the original task completes successfully (either the first time or after retrying transient failures). If the task fails with a non-transient error or the retry limit is reached, the returned task will become faulted and the exception must be observed.

Type Parameters

TResult

The result type of the Task<TResult> returned by operation.

Execute<TResult>(Func<TResult>)

Executes the specified operation and returns the result.

public virtual TResult Execute<TResult>(Func<TResult> operation)

Parameters

operation Func<TResult>

A delegate representing an executable operation that returns the result of type TResult.

Returns

TResult

The result from the operation.

Type Parameters

TResult

The return type of operation.

GetNextDelay(Exception)

Determines whether the operation should be retried and the delay before the next attempt.

protected virtual TimeSpan? GetNextDelay(Exception lastException)

Parameters

lastException Exception

The exception thrown during the last execution attempt.

Returns

TimeSpan?

Returns the delay indicating how long to wait for before the next execution attempt if the operation should be retried; null otherwise

OnFirstExecution()

Method called before the first operation execution

protected virtual void OnFirstExecution()

OnRetry()

Method called before retrying the operation execution

protected virtual void OnRetry()

ShouldRetryOn(Exception)

Determines whether the specified exception represents a transient failure that can be compensated by a retry.

protected abstract bool ShouldRetryOn(Exception exception)

Parameters

exception Exception

The exception object to be verified.

Returns

bool

true if the specified exception is considered as transient, otherwise false.