FibonacciBackoffStrategy Class

Namespace
Kampute.Resilience.Strategies
Assembly
  • Kampute.Resilience.dll

Definition

A retry strategy that increases the delay before each retry attempt based on the Fibonacci sequence.
public sealed class FibonacciBackoffStrategy : IRetryStrategy
Inheritance
  • object
  • FibonacciBackoffStrategy
Implements

Remarks

The FibonacciBackoffStrategy class uses the Fibonacci sequence to adjust the wait time between retries, starting with an initial delay and scaling the delay between subsequent attempts according to Fibonacci numbers. This approach provides a more moderate and controlled increase in delay times compared to the ExponentialBackoffStrategy approach, making it suitable for scenarios where a less aggressive increase in delay is desired.

Constructors

FibonacciBackoffStrategy(TimeSpan)

Initializes a new instance of the FibonacciBackoffStrategy class with a specified initial delay.
public FibonacciBackoffStrategy(TimeSpan initialDelay)

Parameters

initialDelay TimeSpan
The initial delay duration before the first retry attempt.

Exceptions

ArgumentOutOfRangeException
Thrown if initialDelay is negative.

FibonacciBackoffStrategy(TimeSpan, TimeSpan)

Initializes a new instance of the FibonacciBackoffStrategy class with a specified initial delay and step increment.
public FibonacciBackoffStrategy(TimeSpan initialDelay, TimeSpan delayStep)

Parameters

initialDelay TimeSpan
The initial delay duration before the first retry attempt.
delayStep TimeSpan
The fixed amount of time that is scaled by the Fibonacci sequence and added to the initial delay for each subsequent retry attempt.

Exceptions

ArgumentOutOfRangeException
Thrown if initialDelay or delayStep is negative.

Properties

DelayStep

Gets the fixed amount of time that is scaled by the Fibonacci sequence and added to the initial delay for each subsequent retry attempt.
public TimeSpan DelayStep { get; }

Property Value

TimeSpan
The fixed amount of time that is scaled by the Fibonacci sequence and added to the initial delay for each subsequent retry attempt.

InitialDelay

Gets the initial delay duration before the first retry attempt.
public TimeSpan InitialDelay { get; }

Property Value

TimeSpan
The initial delay duration before the first retry attempt.

Methods

TryGetRetryDelay(TimeSpan, uint, out TimeSpan)

Calculates the delay for the next retry attempt based on the Fibonacci sequence.
public bool TryGetRetryDelay(TimeSpan elapsed, uint retryCount, out TimeSpan delay)

Parameters

elapsed TimeSpan
The total time elapsed since the start of retry attempts. This parameter is ignored in this implementation.
retryCount uint
The number of retry attempts made so far.
delay TimeSpan
When this method returns, contains the calculated delay for the next retry attempt. This parameter is passed uninitialized.

Returns

bool
Always returns true, indicating that a retry attempt should be made after the calculated delay.

Extension Methods

Execute(this IRetryStrategy, Action<CancellationToken>, CancellationToken)Runs a blocking operation and retries it as the strategy decides when it throws an exception.
Execute<T>(this IRetryStrategy, Func<CancellationToken, T>, CancellationToken)Runs a blocking operation that returns a value and retries it as the strategy decides when it throws an exception.
Execute<TState>(this IRetryStrategy, TState, Action<TState, CancellationToken>, CancellationToken)Runs a blocking operation that receives a state, and retries it as the strategy decides when it throws an exception.
Execute<TState, T>(this IRetryStrategy, TState, Func<TState, CancellationToken, T>, CancellationToken)Runs a blocking operation that receives a state and returns a value, and retries it as the strategy decides when it throws an exception.
ExecuteAsync(this IRetryStrategy, Func<CancellationToken, Task>, CancellationToken)Runs an asynchronous operation and retries it as the strategy decides when it throws an exception.
ExecuteAsync<T>(this IRetryStrategy, Func<CancellationToken, Task<T>>, CancellationToken)Runs an asynchronous operation that returns a value and retries it as the strategy decides when it throws an exception.
ExecuteAsync<TState>(this IRetryStrategy, TState, Func<TState, CancellationToken, Task>, CancellationToken)Runs an asynchronous operation that receives a state, and retries it as the strategy decides when it throws an exception.
ExecuteAsync<TState, T>(this IRetryStrategy, TState, Func<TState, CancellationToken, Task<T>>, CancellationToken)Runs an asynchronous operation that receives a state and returns a value, and retries it as the strategy decides when it throws an exception.
OnRetry(this IRetryStrategy, Action<RetryContext>)Creates a retry policy that retries every exception and invokes a handler for each approved retry.
OverrideDelay(this IRetryStrategy, Func<RetryContext, TimeSpan>)Creates a retry policy that retries every exception, replacing the strategy's delay with one computed from the failed attempt.
RetryOn<TException>(this IRetryStrategy)Creates a retry policy that retries the exceptions of the specified type as the strategy decides.
RetryOn(this IRetryStrategy, Func<Exception, bool>)Creates a retry policy that retries the exceptions that satisfy a condition, as the strategy decides.
RetryOn<TException>(this IRetryStrategy, Func<TException, bool>)Creates a retry policy that retries the exceptions of the specified type that satisfy a condition, as the strategy decides.
RetryOnResult<T>(this IRetryStrategy, Func<T, bool>)Creates a retry policy for operations returning T that retries every exception and the results that satisfy a condition, as the strategy decides.
StartSession(this IRetryStrategy)Starts a retry session for one operation that the strategy governs.
WithJitter(this IRetryStrategy, double)Adds random jitter to the delays of a retry strategy.
WithMaxDelay(this IRetryStrategy, TimeSpan)Caps each delay of a retry strategy.
WithMaxDelay(this IRetryStrategy, int)Caps each delay of a retry strategy at a number of milliseconds.
WithMaxElapsedTime(this IRetryStrategy, TimeSpan)Limits the time during which a retry strategy allows retries.
WithMaxElapsedTime(this IRetryStrategy, int)Limits the time, in milliseconds, during which a retry strategy allows retries.
WithMaxRetries(this IRetryStrategy, uint)Limits the number of retries a retry strategy allows.