Class PollyResilienceStrategy
- Namespace
- WorkflowForge.Extensions.Resilience.Polly
- Assembly
- WorkflowForge.Extensions.Resilience.Polly.dll
Enterprise-grade resilience strategy that uses Polly for advanced fault tolerance. Supports retry policies, circuit breakers and timeouts.
public sealed class PollyResilienceStrategy : IWorkflowResilienceStrategy
- Inheritance
-
PollyResilienceStrategy
- Implements
- Inherited Members
Properties
Name
Gets the name of the resilience strategy.
public string Name { get; }
Property Value
Methods
CreateCircuitBreakerPolicy(int, TimeSpan?, int, IWorkflowForgeLogger?)
Creates a resilience strategy with circuit breaker policy.
public static PollyResilienceStrategy CreateCircuitBreakerPolicy(int failureThreshold = 5, TimeSpan? durationOfBreak = null, int minimumThroughput = 5, IWorkflowForgeLogger? logger = null)
Parameters
failureThresholdintNumber of failures before opening the circuit.
durationOfBreakTimeSpan?Duration to keep the circuit open.
minimumThroughputintMinimum number of actions in the time window.
loggerIWorkflowForgeLoggerOptional logger for strategy events.
Returns
- PollyResilienceStrategy
A new Polly resilience strategy.
CreateComprehensivePolicy(int, TimeSpan?, int, TimeSpan?, TimeSpan?, IWorkflowForgeLogger?)
Creates a comprehensive resilience strategy combining retry, circuit breaker, and timeout.
public static PollyResilienceStrategy CreateComprehensivePolicy(int maxRetryAttempts = 3, TimeSpan? baseDelay = null, int circuitBreakerThreshold = 5, TimeSpan? circuitBreakerDuration = null, TimeSpan? timeoutDuration = null, IWorkflowForgeLogger? logger = null)
Parameters
maxRetryAttemptsintMaximum number of retry attempts.
baseDelayTimeSpan?Base delay for exponential backoff.
circuitBreakerThresholdintCircuit breaker failure threshold.
circuitBreakerDurationTimeSpan?Circuit breaker open duration.
timeoutDurationTimeSpan?Operation timeout duration.
loggerIWorkflowForgeLoggerOptional logger for strategy events.
Returns
- PollyResilienceStrategy
A new comprehensive Polly resilience strategy.
CreateRetryPolicy(int, TimeSpan?, TimeSpan?, IWorkflowForgeLogger?)
Creates a resilience strategy with retry policy using exponential backoff.
public static PollyResilienceStrategy CreateRetryPolicy(int maxRetryAttempts = 3, TimeSpan? baseDelay = null, TimeSpan? maxDelay = null, IWorkflowForgeLogger? logger = null)
Parameters
maxRetryAttemptsintMaximum number of retry attempts.
baseDelayTimeSpan?Base delay for exponential backoff.
maxDelayTimeSpan?Maximum delay between retries.
loggerIWorkflowForgeLoggerOptional logger for strategy events.
Returns
- PollyResilienceStrategy
A new Polly resilience strategy.
CreateTimeoutPolicy(TimeSpan, IWorkflowForgeLogger?)
Creates a resilience strategy with timeout policy.
public static PollyResilienceStrategy CreateTimeoutPolicy(TimeSpan timeout, IWorkflowForgeLogger? logger = null)
Parameters
timeoutTimeSpanThe timeout duration.
loggerIWorkflowForgeLoggerOptional logger for strategy events.
Returns
- PollyResilienceStrategy
A new Polly resilience strategy.
ExecuteAsync(Func<Task>, CancellationToken)
Executes an operation with resilience handling.
public 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.
ExecuteAsync<T>(Func<Task<T>>, CancellationToken)
Executes an operation with resilience handling and returns a result.
public Task<T> ExecuteAsync<T>(Func<Task<T>> operation, CancellationToken cancellationToken = default)
Parameters
operationFunc<Task<T>>The operation to execute.
cancellationTokenCancellationTokenThe cancellation token.
Returns
- Task<T>
A task representing the asynchronous operation with a result.
Type Parameters
TThe type of the result.
GetRetryDelay(int, Exception?)
Gets the delay before the next retry attempt.
public 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.
ShouldRetryAsync(int, Exception?, CancellationToken)
Determines whether a retry should be attempted based on the current attempt and exception.
public Task<bool> ShouldRetryAsync(int attemptNumber, Exception? exception, CancellationToken cancellationToken = default)
Parameters
attemptNumberintThe current attempt number (1-based).
exceptionExceptionThe exception that occurred, if any.
cancellationTokenCancellationTokenThe cancellation token.