< Summary

Information
Class: TeleFlow.Telegram.Internal.TelegramRawLongPollingBackoff
Assembly: TeleFlow.Telegram.LongPolling
File(s): /_/src/TeleFlow.Telegram.LongPolling/Internal/TelegramRawLongPollingBackoff.cs
Line coverage
65%
Covered lines: 19
Uncovered lines: 10
Coverable lines: 29
Total lines: 83
Line coverage: 65.5%
Branch coverage
50%
Covered branches: 10
Total branches: 20
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ValidateOptions(...)50%411653.84%
NextDelay()50%4470%
Reset()100%11100%
GetJitterMultiplier()100%210%

File(s)

/_/src/TeleFlow.Telegram.LongPolling/Internal/TelegramRawLongPollingBackoff.cs

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2
 3namespace TeleFlow.Telegram.Internal;
 4
 5internal sealed class TelegramRawLongPollingBackoff
 6{
 7    private readonly TelegramRawLongPollingBackoffOptions _options;
 8    private int _attempt;
 9
 10    public TelegramRawLongPollingBackoff(TelegramRawLongPollingBackoffOptions options)
 11    {
 2212        ValidateOptions(options);
 2213        _options = options;
 2214    }
 15
 16    public static void ValidateOptions(TelegramRawLongPollingBackoffOptions? options)
 17    {
 4418        if (options is null)
 19        {
 020            throw new InvalidOperationException("Raw long polling backoff options must be configured.");
 21        }
 22
 4423        if (options.MinDelay < TimeSpan.Zero)
 24        {
 025            throw new InvalidOperationException("Raw long polling backoff minimum delay must not be negative.");
 26        }
 27
 4428        if (options.MaxDelay < TimeSpan.Zero)
 29        {
 030            throw new InvalidOperationException("Raw long polling backoff maximum delay must not be negative.");
 31        }
 32
 4433        if (options.MaxDelay < options.MinDelay)
 34        {
 035            throw new InvalidOperationException("Raw long polling backoff maximum delay must be greater than or equal to
 36        }
 37
 4438        if (options.Factor < 1)
 39        {
 040            throw new InvalidOperationException("Raw long polling backoff factor must be greater than or equal to one.")
 41        }
 42
 4443        if (options.Jitter is < 0 or > 1)
 44        {
 045            throw new InvalidOperationException("Raw long polling backoff jitter must be between zero and one.");
 46        }
 4447    }
 48
 49    public TimeSpan NextDelay()
 50    {
 751        if (!_options.Enabled)
 52        {
 053            return TimeSpan.Zero;
 54        }
 55
 756        var minDelayMilliseconds = _options.MinDelay.TotalMilliseconds;
 757        var maxDelayMilliseconds = _options.MaxDelay.TotalMilliseconds;
 758        var delayMilliseconds = minDelayMilliseconds * Math.Pow(_options.Factor, _attempt);
 59
 760        if (_options.Jitter > 0)
 61        {
 062            var jitterMultiplier = GetJitterMultiplier() * _options.Jitter;
 063            delayMilliseconds += delayMilliseconds * jitterMultiplier;
 64        }
 65
 766        _attempt++;
 767        return TimeSpan.FromMilliseconds(Math.Min(delayMilliseconds, maxDelayMilliseconds));
 68    }
 69
 70    public void Reset()
 71    {
 2972        _attempt = 0;
 2973    }
 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    {
 081        return Random.Shared.NextDouble();
 82    }
 83}