| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | |
| | | 3 | | namespace TeleFlow.Telegram.Internal; |
| | | 4 | | |
| | | 5 | | internal sealed class TelegramRawLongPollingBackoff |
| | | 6 | | { |
| | | 7 | | private readonly TelegramRawLongPollingBackoffOptions _options; |
| | | 8 | | private int _attempt; |
| | | 9 | | |
| | | 10 | | public TelegramRawLongPollingBackoff(TelegramRawLongPollingBackoffOptions options) |
| | | 11 | | { |
| | 22 | 12 | | ValidateOptions(options); |
| | 22 | 13 | | _options = options; |
| | 22 | 14 | | } |
| | | 15 | | |
| | | 16 | | public static void ValidateOptions(TelegramRawLongPollingBackoffOptions? options) |
| | | 17 | | { |
| | 44 | 18 | | if (options is null) |
| | | 19 | | { |
| | 0 | 20 | | throw new InvalidOperationException("Raw long polling backoff options must be configured."); |
| | | 21 | | } |
| | | 22 | | |
| | 44 | 23 | | if (options.MinDelay < TimeSpan.Zero) |
| | | 24 | | { |
| | 0 | 25 | | throw new InvalidOperationException("Raw long polling backoff minimum delay must not be negative."); |
| | | 26 | | } |
| | | 27 | | |
| | 44 | 28 | | if (options.MaxDelay < TimeSpan.Zero) |
| | | 29 | | { |
| | 0 | 30 | | throw new InvalidOperationException("Raw long polling backoff maximum delay must not be negative."); |
| | | 31 | | } |
| | | 32 | | |
| | 44 | 33 | | if (options.MaxDelay < options.MinDelay) |
| | | 34 | | { |
| | 0 | 35 | | throw new InvalidOperationException("Raw long polling backoff maximum delay must be greater than or equal to |
| | | 36 | | } |
| | | 37 | | |
| | 44 | 38 | | if (options.Factor < 1) |
| | | 39 | | { |
| | 0 | 40 | | throw new InvalidOperationException("Raw long polling backoff factor must be greater than or equal to one.") |
| | | 41 | | } |
| | | 42 | | |
| | 44 | 43 | | if (options.Jitter is < 0 or > 1) |
| | | 44 | | { |
| | 0 | 45 | | throw new InvalidOperationException("Raw long polling backoff jitter must be between zero and one."); |
| | | 46 | | } |
| | 44 | 47 | | } |
| | | 48 | | |
| | | 49 | | public TimeSpan NextDelay() |
| | | 50 | | { |
| | 7 | 51 | | if (!_options.Enabled) |
| | | 52 | | { |
| | 0 | 53 | | return TimeSpan.Zero; |
| | | 54 | | } |
| | | 55 | | |
| | 7 | 56 | | var minDelayMilliseconds = _options.MinDelay.TotalMilliseconds; |
| | 7 | 57 | | var maxDelayMilliseconds = _options.MaxDelay.TotalMilliseconds; |
| | 7 | 58 | | var delayMilliseconds = minDelayMilliseconds * Math.Pow(_options.Factor, _attempt); |
| | | 59 | | |
| | 7 | 60 | | if (_options.Jitter > 0) |
| | | 61 | | { |
| | 0 | 62 | | var jitterMultiplier = GetJitterMultiplier() * _options.Jitter; |
| | 0 | 63 | | delayMilliseconds += delayMilliseconds * jitterMultiplier; |
| | | 64 | | } |
| | | 65 | | |
| | 7 | 66 | | _attempt++; |
| | 7 | 67 | | return TimeSpan.FromMilliseconds(Math.Min(delayMilliseconds, maxDelayMilliseconds)); |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | public void Reset() |
| | | 71 | | { |
| | 29 | 72 | | _attempt = 0; |
| | 29 | 73 | | } |
| | | 74 | | |
| | | 75 | | [SuppressMessage( |
| | | 76 | | "Security", |
| | | 77 | | "CA5394:Do not use insecure randomness", |
| | | 78 | | Justification = "Backoff jitter is non-security timing noise and does not require cryptographic randomness.")] |
| | | 79 | | private static double GetJitterMultiplier() |
| | | 80 | | { |
| | 0 | 81 | | return Random.Shared.NextDouble(); |
| | | 82 | | } |
| | | 83 | | } |