Class RetryPolicy
- Namespace
- LMKit.Agents.Resilience
- Assembly
- LM-Kit.NET.dll
A resilience policy that retries failed operations.
Supports configurable retry counts, delays, exponential backoff, and exception filtering.
public sealed class RetryPolicy : IResiliencePolicy
- Inheritance
-
RetryPolicy
- Implements
- Inherited Members
- Extension Methods
Examples
var policy = new RetryPolicy(maxRetries: 3)
.WithExponentialBackoff(TimeSpan.FromSeconds(1), maxDelay: TimeSpan.FromSeconds(30))
.WithJitter(0.2)
.OnRetry((ex, attempt, delay) => Console.WriteLine($"Retry {attempt}: {ex.Message}"));
var result = await policy.ExecuteAsync(ct => agent.RunAsync("Hello", ct));
Constructors
- RetryPolicy(int)
Initializes a new instance of the RetryPolicy class.
Properties
- Default
Creates a retry policy with default settings.
- MaxRetries
Gets the maximum number of retry attempts.
- Name
Gets the name of this policy.
Methods
- ExecuteAsync<T>(Func<ResilienceContext, CancellationToken, Task<T>>, ResilienceContext, CancellationToken)
Executes an action with this policy and context.
- ExecuteAsync<T>(Func<CancellationToken, Task<T>>, CancellationToken)
Executes an action with this policy applied.
- HandleException(Func<Exception, bool>)
Sets a predicate to determine which exceptions should trigger a retry.
- Handle<TException>()
Configures the policy to retry only specific exception types.
- OnRetry(Action<Exception, int, TimeSpan>)
Sets a callback invoked before each retry.
- WithDelay(TimeSpan)
Sets the initial delay between retries.
- WithExponentialBackoff(TimeSpan, double, TimeSpan?)
Configures exponential backoff.
- WithJitter(double)
Adds jitter to prevent thundering herd.