| | | 1 | | namespace Kestrun.Callback; |
| | | 2 | | /// <summary> |
| | | 3 | | /// Default implementation of <see cref="ICallbackRetryPolicy"/> using exponential backoff with jitter. |
| | | 4 | | /// </summary> |
| | | 5 | | /// <remarks> |
| | | 6 | | /// Initializes a new instance of the <see cref="DefaultCallbackRetryPolicy"/> class. |
| | | 7 | | /// </remarks> |
| | | 8 | | public sealed class DefaultCallbackRetryPolicy : ICallbackRetryPolicy |
| | | 9 | | { |
| | | 10 | | private readonly int _maxAttempts; |
| | | 11 | | private readonly TimeSpan _baseDelay; |
| | | 12 | | private readonly TimeSpan _maxDelay; |
| | 15 | 13 | | private readonly Random _rng = new(); |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Initializes a new instance of the <see cref="DefaultCallbackRetryPolicy"/> class with the specified options. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <param name="options"> The options to configure the retry policy. </param> |
| | 15 | 19 | | public DefaultCallbackRetryPolicy(CallbackDispatchOptions? options) |
| | | 20 | | { |
| | 15 | 21 | | if (options == null) |
| | | 22 | | { |
| | 1 | 23 | | _maxAttempts = 3; |
| | 1 | 24 | | _baseDelay = TimeSpan.FromSeconds(2); |
| | 1 | 25 | | _maxDelay = TimeSpan.FromSeconds(30); |
| | 1 | 26 | | return; |
| | | 27 | | } |
| | 14 | 28 | | _maxAttempts = options.MaxAttempts; |
| | 14 | 29 | | _baseDelay = options.BaseDelay; |
| | 14 | 30 | | _maxDelay = options.MaxDelay; |
| | 14 | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// Evaluates the given callback request and result to determine the retry decision. |
| | | 35 | | /// </summary> |
| | | 36 | | /// <param name="req"> The callback request to evaluate. </param> |
| | | 37 | | /// <param name="result"> The result of the callback request. </param> |
| | | 38 | | /// <returns> A decision indicating whether to retry or stop. </returns> |
| | | 39 | | public RetryDecision Evaluate(CallbackRequest req, CallbackResult result) |
| | | 40 | | { |
| | 15 | 41 | | var nextAttemptNumber = req.Attempt + 1; |
| | | 42 | | |
| | 15 | 43 | | if (nextAttemptNumber >= _maxAttempts) |
| | | 44 | | { |
| | 1 | 45 | | return Stop(req, "MaxAttemptsReached"); |
| | | 46 | | } |
| | | 47 | | |
| | 14 | 48 | | if (IsPermanentFailure(result)) |
| | | 49 | | { |
| | 6 | 50 | | return Stop(req, "PermanentFailure"); |
| | | 51 | | } |
| | | 52 | | |
| | 8 | 53 | | if (!IsRetryable(result)) |
| | | 54 | | { |
| | 3 | 55 | | return Stop(req, "NotRetryable"); |
| | | 56 | | } |
| | | 57 | | |
| | 5 | 58 | | var delay = ComputeBackoff(nextAttemptNumber); |
| | 5 | 59 | | var next = DateTimeOffset.UtcNow.Add(delay); |
| | | 60 | | |
| | | 61 | | // Respect Retry-After if you capture it (nice-to-have) |
| | | 62 | | // if (result.RetryAfter != null) { ... } |
| | | 63 | | |
| | 5 | 64 | | return new RetryDecision(RetryDecisionKind.Retry, next, delay, "RetryableFailure"); |
| | | 65 | | } |
| | | 66 | | /// <summary> |
| | | 67 | | /// Creates a Stop retry decision with the given reason. |
| | | 68 | | /// </summary> |
| | | 69 | | /// <param name="req"> The callback request for which to create the stop decision. </param> |
| | | 70 | | /// <param name="reason"> The reason for stopping retries. </param> |
| | | 71 | | /// <returns>A RetryDecision indicating to stop retries.</returns> |
| | | 72 | | private static RetryDecision Stop(CallbackRequest req, string reason) |
| | 10 | 73 | | => new(RetryDecisionKind.Stop, req.NextAttemptAt, TimeSpan.Zero, reason); |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// Determines if the callback result indicates a permanent failure. |
| | | 77 | | /// </summary> |
| | | 78 | | /// <param name="r">The callback result to evaluate.</param> |
| | | 79 | | /// <returns>True if the result indicates a permanent failure; otherwise, false.</returns> |
| | | 80 | | /// <remarks> |
| | | 81 | | /// Permanent failures are those that should not be retried. |
| | | 82 | | /// could treat repeated 401/403 as permanent immediately |
| | | 83 | | /// </remarks> |
| | | 84 | | private static bool IsPermanentFailure(CallbackResult r) => |
| | 14 | 85 | | r.StatusCode is 400 or 401 or 403 or 404 or 409 or 422; |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Determines if the callback result indicates a retryable failure. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="r">The callback result to evaluate.</param> |
| | | 91 | | /// <returns>True if the result indicates a retryable failure; otherwise, false.</returns> |
| | | 92 | | private static bool IsRetryable(CallbackResult r) |
| | | 93 | | { |
| | 8 | 94 | | if (r.ErrorType is "Timeout" or "HttpRequestException") |
| | | 95 | | { |
| | 1 | 96 | | return true; |
| | | 97 | | } |
| | | 98 | | |
| | 7 | 99 | | if (r.StatusCode is null) |
| | | 100 | | { |
| | 0 | 101 | | return false; |
| | | 102 | | } |
| | | 103 | | |
| | 7 | 104 | | var sc = r.StatusCode.Value; |
| | 7 | 105 | | return sc is 408 or 429 or (>= 500 and <= 599); |
| | | 106 | | } |
| | | 107 | | /// <summary> |
| | | 108 | | /// Computes the exponential backoff delay with jitter based on the attempt number. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <param name="attemptNumber">The current attempt number (1-based).</param> |
| | | 111 | | /// <returns>A TimeSpan representing the delay before the next retry.</returns> |
| | | 112 | | private TimeSpan ComputeBackoff(int attemptNumber) |
| | | 113 | | { |
| | | 114 | | // attemptNumber: 1..N |
| | 5 | 115 | | var exp = Math.Pow(2, attemptNumber - 1); |
| | 5 | 116 | | var raw = TimeSpan.FromMilliseconds(_baseDelay.TotalMilliseconds * exp); |
| | | 117 | | |
| | 5 | 118 | | var capped = raw <= _maxDelay ? raw : _maxDelay; |
| | 5 | 119 | | var jitter = TimeSpan.FromMilliseconds(_rng.Next(0, 250)); |
| | 5 | 120 | | return capped + jitter; |
| | | 121 | | } |
| | | 122 | | } |