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
baseDelayTimeSpanThe base delay for the first retry attempt.
maxDelayTimeSpanThe maximum delay between retry attempts.
maxAttemptsintThe maximum number of attempts (including the initial attempt).
backoffMultiplierdoubleThe multiplier for the exponential backoff (default: 2.0).
retryPredicateFunc<Exception, bool>Optional predicate to determine if an exception should be retried.
enableJitterboolWhether to enable jitter in the retry delay.
loggerIWorkflowForgeLoggerOptional 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
maxAttemptsintMaximum number of retry attempts.
loggerIWorkflowForgeLoggerOptional 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
operationFunc<Task>The operation to execute.
cancellationTokenCancellationTokenThe 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
maxAttemptsintMaximum number of retry attempts.
loggerIWorkflowForgeLoggerOptional 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
attemptNumberintThe current attempt number (1-based).
exceptionExceptionThe exception that occurred, if any.
Returns
- TimeSpan
The delay before the next retry attempt.