Table of Contents

Class ExponentialBackoffStrategy

Namespace
WorkflowForge.Extensions.Resilience.Strategies
Assembly
WorkflowForge.Extensions.Resilience.dll

Implements an exponential backoff resilience strategy with configurable base delay, multiplier, and jitter.

public sealed class ExponentialBackoffStrategy : ResilienceStrategyBase, IWorkflowResilienceStrategy
Inheritance
ExponentialBackoffStrategy
Implements
Inherited Members

Constructors

ExponentialBackoffStrategy(TimeSpan, TimeSpan, int, double, Func<Exception, bool>?, bool, IWorkflowForgeLogger?)

Initializes a new instance of the ExponentialBackoffStrategy class.

public ExponentialBackoffStrategy(TimeSpan baseDelay, TimeSpan maxDelay, int maxAttempts, double backoffMultiplier = 2, Func<Exception, bool>? retryPredicate = null, bool enableJitter = true, IWorkflowForgeLogger? logger = null)

Parameters

baseDelay TimeSpan

The base delay for the first retry attempt.

maxDelay TimeSpan

The maximum delay between retry attempts.

maxAttempts int

The maximum number of attempts (including the initial attempt).

backoffMultiplier double

The multiplier for the exponential backoff (default: 2.0).

retryPredicate Func<Exception, bool>

Optional predicate to determine if an exception should be retried.

enableJitter bool

Whether to enable jitter in the retry delay.

logger IWorkflowForgeLogger

Optional logger for the strategy.

Methods

Default(int, IWorkflowForgeLogger?)

Creates a default exponential backoff strategy with sensible defaults.

public static ExponentialBackoffStrategy Default(int maxAttempts = 3, IWorkflowForgeLogger? logger = null)

Parameters

maxAttempts int

Maximum number of retry attempts.

logger IWorkflowForgeLogger

Optional logger for strategy events.

Returns

ExponentialBackoffStrategy

A new exponential backoff strategy.

ExecuteAsync(Func<Task>, CancellationToken)

Executes an operation with resilience handling.

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

Parameters

operation Func<Task>

The operation to execute.

cancellationToken CancellationToken

The cancellation token.

Returns

Task

A task representing the asynchronous operation.

ForTransientErrors(int, IWorkflowForgeLogger?)

Creates an exponential backoff strategy optimized for transient errors.

public static ExponentialBackoffStrategy ForTransientErrors(int maxAttempts = 3, IWorkflowForgeLogger? logger = null)

Parameters

maxAttempts int

Maximum number of retry attempts.

logger IWorkflowForgeLogger

Optional logger for strategy events.

Returns

ExponentialBackoffStrategy

A new exponential backoff strategy.

GetRetryDelay(int, Exception?)

Gets the delay before the next retry attempt.

public override TimeSpan GetRetryDelay(int attemptNumber, Exception? exception)

Parameters

attemptNumber int

The current attempt number (1-based).

exception Exception

The exception that occurred, if any.

Returns

TimeSpan

The delay before the next retry attempt.