Path: redb-route/redb.Route/ErrorHandling/RetryPolicy.cs Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route/ErrorHandling/RetryPolicy.cs ---- namespace redb.Route.ErrorHandling; /// /// Defines a policy for retrying failed exchange processing. /// public sealed class RetryPolicy { /// Maximum number of retry attempts. 0 means no retries. public int MaxRetries { get; init; } /// Initial delay between retries. public TimeSpan InitialDelay { get; init; } = TimeSpan.FromMilliseconds(100); /// /// Multiplier for exponential backoff between retries. /// 1.0 = fixed delay, 2.0 = double each time. /// public double BackoffMultiplier { get; init; } = 2.0; /// Maximum delay cap between retries. public TimeSpan MaxDelay { get; init; } = TimeSpan.FromSeconds(30); /// /// Collision avoidance factor for adding random jitter to delays. /// 0.0 = no jitter, 0.15 = ±15% of the calculated delay (Camel default). /// Jitter is applied after exponential/fixed calculation but before the max-delay cap. /// Uses for thread safety. /// public double CollisionAvoidanceFactor { get; init; } /// /// Delay pattern string that overrides exponential/fixed calculation for specific attempt ranges. /// Format: "attempt1:delayMs;attempt2:delayMs;..." (e.g., "1:1000;5:5000;10:30000"). /// If the current attempt matches or exceeds a key, that delay is used. /// The highest matching key wins. When no key matches, standard calculation is used. /// public string? DelayPattern { get; init; } /// /// Predicate to determine if an exception is retryable. /// When null, all exceptions (except ) are retried. /// public Func? RetryableExceptionPredicate { get; init; } /// Creates a retry policy for no retries (fail immediately). public static RetryPolicy None => new() { MaxRetries = 0 }; /// Creates a fixed-delay retry policy. /// Maximum retry attempts. /// Fixed delay between retries. public static RetryPolicy Fixed(int maxRetries, TimeSpan delay) => new() { MaxRetries = maxRetries, InitialDelay = delay, BackoffMultiplier = 1.0, MaxDelay = delay }; /// Creates an exponential backoff retry policy. /// Maximum retry attempts. /// Initial delay. /// Backoff multiplier (default: 2.0). /// Maximum delay cap. public static RetryPolicy Exponential( int maxRetries, TimeSpan initialDelay, double multiplier = 2.0, TimeSpan? maxDelay = null) => new() { MaxRetries = maxRetries, InitialDelay = initialDelay, BackoffMultiplier = multiplier, MaxDelay = maxDelay ?? TimeSpan.FromSeconds(60) }; /// Calculates the delay for the given attempt number (0-based). /// Attempt number (0 = first retry). /// Delay duration for this attempt. public TimeSpan GetDelay(int attempt) { // DelayPattern takes priority when set and a matching key exists if (DelayPattern is not null) { var patternDelay = ResolveDelayPattern(attempt); if (patternDelay.HasValue) return patternDelay.Value; } double delayMs; if (attempt <= 0) delayMs = InitialDelay.TotalMilliseconds; else delayMs = InitialDelay.TotalMilliseconds * Math.Pow(BackoffMultiplier, attempt); // Apply jitter before cap if (CollisionAvoidanceFactor > 0) { var jitterRange = delayMs * CollisionAvoidanceFactor; delayMs += (Random.Shared.NextDouble() * 2 - 1) * jitterRange; delayMs = Math.Max(0, delayMs); } var capped = Math.Min(delayMs, MaxDelay.TotalMilliseconds); return TimeSpan.FromMilliseconds(capped); } private TimeSpan? ResolveDelayPattern(int attempt) { // Format: "1:1000;5:5000;10:30000" // Find the highest key <= attempt+1 (1-based in the pattern) var oneBasedAttempt = attempt + 1; double? bestDelay = null; int bestKey = -1; foreach (var part in DelayPattern!.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)) { var colonIdx = part.IndexOf(':'); if (colonIdx <= 0) continue; if (!int.TryParse(part.AsSpan(0, colonIdx), out var key)) continue; if (!double.TryParse(part.AsSpan(colonIdx + 1), out var delayMs)) continue; if (key <= oneBasedAttempt && key > bestKey) { bestKey = key; bestDelay = delayMs; } } return bestDelay.HasValue ? TimeSpan.FromMilliseconds(bestDelay.Value) : null; } /// Returns whether the exception is eligible for retry. /// The exception to evaluate. /// true if the exception should be retried. public bool ShouldRetry(Exception ex) { if (ex is OperationCanceledException) return false; return RetryableExceptionPredicate?.Invoke(ex) ?? true; } }