< Summary

Information
Class: ArturRios.Util.FlowControl.Waiter.Backoff
Assembly: ArturRios.Util
File(s): /home/runner/work/dotnet-util/dotnet-util/src/FlowControl/Waiter/Backoff.cs
Line coverage
100%
Covered lines: 8
Uncovered lines: 0
Coverable lines: 8
Total lines: 41
Line coverage: 100%
Branch coverage
83%
Covered branches: 5
Total branches: 6
Branch coverage: 83.3%
Method coverage
100%
Covered methods: 1
Fully covered methods: 1
Total methods: 1
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
DelayFor(...)83.33%66100%

File(s)

/home/runner/work/dotnet-util/dotnet-util/src/FlowControl/Waiter/Backoff.cs

#LineLine coverage
 1namespace ArturRios.Util.FlowControl.Waiter;
 2
 3/// <summary>
 4/// The exponential-backoff-with-jitter schedule shared by <see cref="JitteredWaiter"/> and
 5/// <see cref="Retry"/>, so the two cannot drift apart.
 6/// </summary>
 7internal static class Backoff
 8{
 9    /// <summary>
 10    /// Wait applied before the first retry, and the floor every later wait builds on.
 11    /// </summary>
 12    internal const int FixedWaitDelay = 500;
 13
 14    /// <summary>
 15    /// Computes the delay for a zero-based retry attempt: half the backoff period plus a random half.
 16    /// </summary>
 17    /// <param name="attempt">The zero-based retry attempt.</param>
 18    /// <param name="maxWaitMilliseconds">Ceiling applied to the result.</param>
 19    /// <remarks>
 20    /// The exponent is computed in 64-bit arithmetic and clamped before it is narrowed, so an attempt
 21    /// index large enough to overflow a 32-bit millisecond count simply saturates at
 22    /// <paramref name="maxWaitMilliseconds"/> instead of throwing.
 23    /// </remarks>
 24    internal static int DelayFor(int attempt, int maxWaitMilliseconds)
 25    {
 8226        if (attempt <= 0)
 27        {
 1128            return System.Math.Min(FixedWaitDelay, maxWaitMilliseconds);
 29        }
 30
 31        // 2^52 milliseconds already dwarfs any sane ceiling, so shifting further would only overflow.
 7132        var backoffPeriodMs = attempt >= 52
 7133            ? long.MaxValue / 2L
 7134            : (1L << attempt) * 1000L - FixedWaitDelay;
 35
 7136        var half = backoffPeriodMs / 2L;
 7137        var jitter = half > 0L ? System.Random.Shared.NextInt64(0L, half) : 0L;
 38
 7139        return (int)System.Math.Min(FixedWaitDelay + half + jitter, maxWaitMilliseconds);
 40    }
 41}