using System.Collections.Generic; namespace OpenWaf.Domain.Engine; /// /// Live in-memory state for one source. Mutable and lock-guarded on purpose: /// this object is touched on every response, so it must not allocate a fresh /// record per request. /// public sealed class StreakState { public required string SourceKey { get; init; } public int ConsecutiveRefusals; public int MaxConsecutiveRefusals; public int TotalRefusals; public int ConsecutiveServerErrors; public int MaxConsecutiveServerErrors; /// The count the last alert of each kind was raised at, for this /// episode. 0 means "no alert yet", which is what makes the crossing /// itself always speak. Reset when the episode ends (a clean answer, or a /// gap longer than the window), so the next episode gets its own alert. /// See StreakTracker.IsEscalation for what counts as escalation. public int LastAlertedRefusals; public int LastAlertedServerErrors; public DateTimeOffset? LastRefusalAt; public DateTimeOffset LastSeenAt; public int? LastStatusCode; public string? LastPath; public string? LastUserAgent; public string? LastCountryCode; public string? LastCity; public string? LastRegion; /// Position in the LRU list, so eviction is O(1) and a touch is /// O(1). Null once the state has been evicted. public LinkedListNode? LruNode; public bool HasEvidence => TotalRefusals > 0 || MaxConsecutiveServerErrors > 0; public void Apply(RequestEvidence evidence) { if (evidence.Path is not null) LastPath = evidence.Path; if (evidence.UserAgent is not null) LastUserAgent = evidence.UserAgent; if (evidence.CountryCode is not null) LastCountryCode = evidence.CountryCode; if (evidence.City is not null) LastCity = evidence.City; if (evidence.Region is not null) LastRegion = evidence.Region; } }