| | | 1 | | namespace ProjectTemplate.Web.Diagnostics; |
| | | 2 | | |
| | | 3 | | /// <summary> |
| | | 4 | | /// Limits how often the rate-limiting unknown-client fallback warning is written, while counting the |
| | | 5 | | /// occurrences that were suppressed between warnings. |
| | | 6 | | /// </summary> |
| | | 7 | | /// <remarks> |
| | | 8 | | /// The fallback path runs on every affected request. Without throttling, a deployment where |
| | | 9 | | /// <c>RemoteIpAddress</c> is never available would write one warning per request. |
| | | 10 | | /// </remarks> |
| | | 11 | | internal sealed class RateLimitingFallbackWarningThrottle |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// The default minimum interval between fallback warnings. |
| | | 15 | | /// </summary> |
| | 1 | 16 | | internal static readonly TimeSpan DefaultInterval = TimeSpan.FromMinutes(1); |
| | | 17 | | |
| | | 18 | | private readonly TimeProvider _timeProvider; |
| | | 19 | | private readonly long _intervalTimestampTicks; |
| | 109 | 20 | | private long _nextWarningTimestamp = long.MinValue; |
| | | 21 | | private long _suppressedCount; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="RateLimitingFallbackWarningThrottle"/> class. |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="timeProvider">The time source used to measure the warning interval.</param> |
| | | 27 | | /// <param name="interval">The minimum interval between warnings. Must be greater than zero.</param> |
| | 109 | 28 | | internal RateLimitingFallbackWarningThrottle(TimeProvider timeProvider, TimeSpan interval) |
| | | 29 | | { |
| | 109 | 30 | | ArgumentNullException.ThrowIfNull(timeProvider); |
| | 109 | 31 | | ArgumentOutOfRangeException.ThrowIfLessThanOrEqual(interval, TimeSpan.Zero); |
| | | 32 | | |
| | 108 | 33 | | _timeProvider = timeProvider; |
| | 108 | 34 | | _intervalTimestampTicks = checked((long)(interval.TotalSeconds * timeProvider.TimestampFrequency)); |
| | 108 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Determines whether a fallback warning may be written now. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="suppressedCount"> |
| | | 41 | | /// When this method returns <see langword="true"/>, the number of warnings suppressed since the previous |
| | | 42 | | /// warning; otherwise zero. |
| | | 43 | | /// </param> |
| | | 44 | | /// <returns><see langword="true"/> when the caller should write the warning; otherwise <see langword="false"/>.</re |
| | | 45 | | internal bool TryAcquire(out long suppressedCount) |
| | | 46 | | { |
| | 108 | 47 | | long now = _timeProvider.GetTimestamp(); |
| | 108 | 48 | | long nextWarningTimestamp = Interlocked.Read(ref _nextWarningTimestamp); |
| | | 49 | | |
| | 108 | 50 | | if (now >= nextWarningTimestamp && |
| | 108 | 51 | | Interlocked.CompareExchange( |
| | 108 | 52 | | ref _nextWarningTimestamp, |
| | 108 | 53 | | now + _intervalTimestampTicks, |
| | 108 | 54 | | nextWarningTimestamp) == nextWarningTimestamp) |
| | | 55 | | { |
| | 72 | 56 | | suppressedCount = Interlocked.Exchange(ref _suppressedCount, 0); |
| | 72 | 57 | | return true; |
| | | 58 | | } |
| | | 59 | | |
| | 36 | 60 | | _ = Interlocked.Increment(ref _suppressedCount); |
| | 36 | 61 | | suppressedCount = 0; |
| | 36 | 62 | | return false; |
| | | 63 | | } |
| | | 64 | | } |