|
| 1 | +using System.Text.RegularExpressions; |
| 2 | + |
| 3 | +namespace Amazon.Lambda.DurableExecution; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Jitter strategy for exponential backoff to prevent thundering-herd scenarios. |
| 7 | +/// </summary> |
| 8 | +public enum JitterStrategy |
| 9 | +{ |
| 10 | + /// <summary>No randomization — delay is exactly the calculated backoff value.</summary> |
| 11 | + None, |
| 12 | + /// <summary>Random delay between 0 and the calculated backoff value (recommended).</summary> |
| 13 | + Full, |
| 14 | + /// <summary>Random delay between 50% and 100% of the calculated backoff value.</summary> |
| 15 | + Half |
| 16 | +} |
| 17 | + |
| 18 | +/// <summary> |
| 19 | +/// Controls whether a step re-executes if the Lambda is re-invoked mid-attempt. |
| 20 | +/// </summary> |
| 21 | +public enum StepSemantics |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Default. The step may re-execute if the Lambda is re-invoked during execution. |
| 25 | + /// Use for idempotent operations. |
| 26 | + /// </summary> |
| 27 | + AtLeastOncePerRetry, |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// The step executes at most once per retry attempt. A START checkpoint is written |
| 31 | + /// before execution; on replay with an existing START, the SDK skips re-execution |
| 32 | + /// and proceeds to the retry handler. |
| 33 | + /// </summary> |
| 34 | + AtMostOncePerRetry |
| 35 | +} |
| 36 | + |
| 37 | +/// <summary> |
| 38 | +/// Factory methods for common retry strategies. |
| 39 | +/// </summary> |
| 40 | +public static class RetryStrategy |
| 41 | +{ |
| 42 | + /// <summary>6 attempts, 2x backoff, 5s initial delay, 60s max, Full jitter.</summary> |
| 43 | + public static IRetryStrategy Default { get; } = Exponential( |
| 44 | + maxAttempts: 6, |
| 45 | + initialDelay: TimeSpan.FromSeconds(5), |
| 46 | + maxDelay: TimeSpan.FromSeconds(60), |
| 47 | + backoffRate: 2.0, |
| 48 | + jitter: JitterStrategy.Full); |
| 49 | + |
| 50 | + /// <summary>3 attempts, 2x backoff, 1s initial delay, 5s max, Half jitter.</summary> |
| 51 | + public static IRetryStrategy Transient { get; } = Exponential( |
| 52 | + maxAttempts: 3, |
| 53 | + initialDelay: TimeSpan.FromSeconds(1), |
| 54 | + maxDelay: TimeSpan.FromSeconds(5), |
| 55 | + backoffRate: 2.0, |
| 56 | + jitter: JitterStrategy.Half); |
| 57 | + |
| 58 | + /// <summary>No retry — 1 attempt only.</summary> |
| 59 | + public static IRetryStrategy None { get; } = Exponential(maxAttempts: 1); |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Creates an exponential backoff retry strategy. |
| 63 | + /// </summary> |
| 64 | + public static IRetryStrategy Exponential( |
| 65 | + int maxAttempts = 3, |
| 66 | + TimeSpan? initialDelay = null, |
| 67 | + TimeSpan? maxDelay = null, |
| 68 | + double backoffRate = 2.0, |
| 69 | + JitterStrategy jitter = JitterStrategy.Full, |
| 70 | + Type[]? retryableExceptions = null, |
| 71 | + string[]? retryableMessagePatterns = null) |
| 72 | + { |
| 73 | + return new ExponentialRetryStrategy( |
| 74 | + maxAttempts, |
| 75 | + initialDelay ?? TimeSpan.FromSeconds(5), |
| 76 | + maxDelay ?? TimeSpan.FromSeconds(300), |
| 77 | + backoffRate, |
| 78 | + jitter, |
| 79 | + retryableExceptions, |
| 80 | + retryableMessagePatterns); |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Creates a retry strategy from a delegate. |
| 85 | + /// </summary> |
| 86 | + public static IRetryStrategy FromDelegate(Func<Exception, int, RetryDecision> strategy) |
| 87 | + => new DelegateRetryStrategy(strategy); |
| 88 | +} |
| 89 | + |
| 90 | +internal sealed class ExponentialRetryStrategy : IRetryStrategy |
| 91 | +{ |
| 92 | + private readonly int _maxAttempts; |
| 93 | + private readonly TimeSpan _initialDelay; |
| 94 | + private readonly TimeSpan _maxDelay; |
| 95 | + private readonly double _backoffRate; |
| 96 | + private readonly JitterStrategy _jitter; |
| 97 | + private readonly Type[]? _retryableExceptions; |
| 98 | + private readonly Regex[]? _retryableMessagePatterns; |
| 99 | + |
| 100 | + [ThreadStatic] |
| 101 | + private static Random? t_random; |
| 102 | + private static Random Random => t_random ??= new Random(); |
| 103 | + |
| 104 | + public ExponentialRetryStrategy( |
| 105 | + int maxAttempts, |
| 106 | + TimeSpan initialDelay, |
| 107 | + TimeSpan maxDelay, |
| 108 | + double backoffRate, |
| 109 | + JitterStrategy jitter, |
| 110 | + Type[]? retryableExceptions, |
| 111 | + string[]? retryableMessagePatterns) |
| 112 | + { |
| 113 | + _maxAttempts = maxAttempts; |
| 114 | + _initialDelay = initialDelay; |
| 115 | + _maxDelay = maxDelay; |
| 116 | + _backoffRate = backoffRate; |
| 117 | + _jitter = jitter; |
| 118 | + _retryableExceptions = retryableExceptions; |
| 119 | + _retryableMessagePatterns = retryableMessagePatterns? |
| 120 | + .Select(p => new Regex(p, RegexOptions.Compiled)) |
| 121 | + .ToArray(); |
| 122 | + } |
| 123 | + |
| 124 | + public RetryDecision ShouldRetry(Exception exception, int attemptNumber) |
| 125 | + { |
| 126 | + if (attemptNumber >= _maxAttempts) |
| 127 | + return RetryDecision.DoNotRetry(); |
| 128 | + |
| 129 | + if (!IsRetryable(exception)) |
| 130 | + return RetryDecision.DoNotRetry(); |
| 131 | + |
| 132 | + var delay = CalculateDelay(attemptNumber); |
| 133 | + return RetryDecision.RetryAfter(delay); |
| 134 | + } |
| 135 | + |
| 136 | + private bool IsRetryable(Exception exception) |
| 137 | + { |
| 138 | + if (_retryableExceptions == null && _retryableMessagePatterns == null) |
| 139 | + return true; |
| 140 | + |
| 141 | + if (_retryableExceptions != null) |
| 142 | + { |
| 143 | + var exType = exception.GetType(); |
| 144 | + if (_retryableExceptions.Any(t => t.IsAssignableFrom(exType))) |
| 145 | + return true; |
| 146 | + } |
| 147 | + |
| 148 | + if (_retryableMessagePatterns != null) |
| 149 | + { |
| 150 | + var message = exception.Message; |
| 151 | + if (_retryableMessagePatterns.Any(p => p.IsMatch(message))) |
| 152 | + return true; |
| 153 | + } |
| 154 | + |
| 155 | + return false; |
| 156 | + } |
| 157 | + |
| 158 | + internal TimeSpan CalculateDelay(int attemptNumber) |
| 159 | + { |
| 160 | + var baseDelay = _initialDelay.TotalSeconds * Math.Pow(_backoffRate, attemptNumber - 1); |
| 161 | + var cappedDelay = Math.Min(baseDelay, _maxDelay.TotalSeconds); |
| 162 | + |
| 163 | + var finalDelay = _jitter switch |
| 164 | + { |
| 165 | + JitterStrategy.Full => Random.NextDouble() * cappedDelay, |
| 166 | + JitterStrategy.Half => cappedDelay * (0.5 + 0.5 * Random.NextDouble()), |
| 167 | + _ => cappedDelay |
| 168 | + }; |
| 169 | + |
| 170 | + return TimeSpan.FromSeconds(Math.Max(1, Math.Ceiling(finalDelay))); |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +internal sealed class DelegateRetryStrategy : IRetryStrategy |
| 175 | +{ |
| 176 | + private readonly Func<Exception, int, RetryDecision> _strategy; |
| 177 | + |
| 178 | + public DelegateRetryStrategy(Func<Exception, int, RetryDecision> strategy) |
| 179 | + { |
| 180 | + _strategy = strategy; |
| 181 | + } |
| 182 | + |
| 183 | + public RetryDecision ShouldRetry(Exception exception, int attemptNumber) |
| 184 | + => _strategy(exception, attemptNumber); |
| 185 | +} |
0 commit comments