namespace OpenWaf.Infrastructure.Persistence; /// /// When the next attempt at a delivery is allowed, and when to stop (OPEN-60). /// /// Pure, because the two failure modes of a retry loop are both arithmetic: a /// loop that never stops turns an outage at the receiver into load here, and a /// loop that stops after one try is the at-most-once behaviour this item exists /// to remove. Neither needs a running edge to check. /// /// The bound is deliberate. An endpoint that has refused for an hour is broken, /// and the honest answer at that point is to say so on the surface rather than to /// keep asking — which is why giving up is a recorded state and not a dropped row. /// public static class PushRetryPolicy { public const int MaxAttemptsFloor = 1, MaxAttemptsCeiling = 20; public const int RetrySecondsFloor = 1, RetrySecondsCeiling = 3600; /// The cap on a single backoff step. Exponential growth from a /// 30-second base reaches a day in eight steps, and a day is not a retry /// interval for a page that was supposed to fire at 03:00. public static readonly TimeSpan MaxDelay = TimeSpan.FromMinutes(30); /// How long a claim may be held before another process may take the /// row over. Longer than one push timeout times the attempts inside one push, /// shorter than the retry interval, so a crashed process delays a delivery /// rather than losing it. public static readonly TimeSpan ClaimReclaimWindow = TimeSpan.FromMinutes(5); public static int ParseMaxAttempts(string? configured) { if (string.IsNullOrWhiteSpace(configured)) return 6; if (!int.TryParse(configured, out var attempts) || attempts < MaxAttemptsFloor || attempts > MaxAttemptsCeiling) throw new InvalidOperationException( $"OpenWaf:Alerts:MaxPushAttempts must be a whole number of attempts between " + $"{MaxAttemptsFloor} and {MaxAttemptsCeiling}; got {configured}"); return attempts; } public static int ParseRetrySeconds(string? configured) { if (string.IsNullOrWhiteSpace(configured)) return 30; if (!int.TryParse(configured, out var seconds) || seconds < RetrySecondsFloor || seconds > RetrySecondsCeiling) throw new InvalidOperationException( $"OpenWaf:Alerts:PushRetrySeconds must be a whole number of seconds between " + $"{RetrySecondsFloor} and {RetrySecondsCeiling}; got {configured}"); return seconds; } /// The delay before attempt number attempts + 1, given that /// attempts have already failed. Exponential from the base, capped, and /// never zero — a zero delay against a dead endpoint is a busy loop. public static TimeSpan NextDelay(int attempts, int baseSeconds) { if (attempts <= 0) return TimeSpan.Zero; var exponent = Math.Min(attempts, 16); var seconds = (double)baseSeconds * Math.Pow(2, exponent - 1); return TimeSpan.FromSeconds(Math.Min(seconds, MaxDelay.TotalSeconds)); } /// Has this delivery run out of attempts? Compared against the /// configured bound rather than a constant, because the bound is /// configuration and a test has to be able to ask for a small one. public static bool HasGivenUp(int attempts, int maxAttempts) => attempts >= maxAttempts; }