fix(drivers): #999 K-10 a backoff cap below the poll interval never polls a failing device faster than a healthy one
This commit is contained in:
@@ -42,7 +42,8 @@ public sealed class ConnectionBackoff
|
||||
/// <summary>
|
||||
/// Capped exponential backoff schedule. <paramref name="consecutiveFailures"/> ≤ 0 returns
|
||||
/// <paramref name="baseInterval"/>; each subsequent failure doubles the wait up to
|
||||
/// <paramref name="cap"/>. Computed in ticks with an overflow guard so a large failure
|
||||
/// <paramref name="cap"/> — or up to <paramref name="baseInterval"/> when the cap is smaller (a failure never
|
||||
/// shortens the wait, #999 / K-10). Computed in ticks with an overflow guard so a large failure
|
||||
/// count saturates at the cap rather than wrapping negative. Ported verbatim from the S7
|
||||
/// poll fork's <c>ComputeBackoffDelay</c>.
|
||||
/// </summary>
|
||||
@@ -53,6 +54,10 @@ public sealed class ConnectionBackoff
|
||||
public static TimeSpan ComputeDelay(TimeSpan baseInterval, int consecutiveFailures, TimeSpan cap)
|
||||
{
|
||||
if (consecutiveFailures <= 0) return baseInterval;
|
||||
// #999 / review K-10: the cap bounds the GROWTH, it never undercuts the base. As an absolute ceiling a cap below
|
||||
// the base (every poll driver passes 30 s; a Sql subscription may poll every 60 s) retried a FAILING device
|
||||
// faster than a healthy one — the opposite of backoff.
|
||||
if (cap < baseInterval) cap = baseInterval;
|
||||
// Cap the shift to avoid overflow — at 30 the result already saturates the cap for any
|
||||
// reasonable base interval.
|
||||
var shift = Math.Min(consecutiveFailures - 1, 30);
|
||||
|
||||
@@ -107,11 +107,11 @@ public sealed class PollGroupEngine : IAsyncDisposable
|
||||
/// <para>The <c>interval</c> feeding that schedule is the per-subscription interval
|
||||
/// <i>already clamped</i> to <paramref name="minInterval"/> in <see cref="Subscribe(IReadOnlyList{string}, TimeSpan)"/>, so
|
||||
/// the first failure's wait equals the clamped interval (shift 0 → 1×) and the floor is
|
||||
/// honoured for free. One asymmetry worth knowing: <c>ComputeDelay</c> saturates at the cap
|
||||
/// unconditionally, so a <paramref name="backoffCap"/> set SMALLER than the clamped
|
||||
/// interval makes failures retry FASTER than the interval floor — the cap outranks
|
||||
/// <paramref name="minInterval"/> on the failure branch. Configure it above the floor
|
||||
/// (every driver here uses 30 s against a 100 ms floor).</para></param>
|
||||
/// honoured for free. A <paramref name="backoffCap"/> SMALLER than a subscription's clamped
|
||||
/// interval never shortens it (#999 / review K-10): <c>ComputeDelay</c> raises the cap to the
|
||||
/// base, so that subscription's failure branch waits exactly its interval — a failing device is
|
||||
/// never polled faster than a healthy one. (Every driver here uses 30 s, so a 60 s Sql
|
||||
/// subscription used to retry every 30 s once it failed.)</para></param>
|
||||
/// <param name="onBatch">Optional per-cycle batch callback — invoked once per poll cycle that
|
||||
/// produced at least one change, carrying exactly that cycle's changed items in reader
|
||||
/// order, after all of the cycle's per-tag <paramref name="onChange"/> callbacks. The
|
||||
|
||||
@@ -42,6 +42,18 @@ public sealed class ConnectionBackoffTests
|
||||
public void ComputeDelay_SaturatesAtCap(int failures)
|
||||
=> ConnectionBackoff.ComputeDelay(Base, failures, Cap).ShouldBe(Cap);
|
||||
|
||||
/// <summary>
|
||||
/// #999 / review K-10: a cap BELOW the base interval never shortens it. The cap used to be an absolute ceiling, so
|
||||
/// a 60 s subscription behind every poll driver's 30 s cap was polled every 30 s once it started failing — faster
|
||||
/// than when it was healthy.
|
||||
/// </summary>
|
||||
[Theory]
|
||||
[InlineData(1)]
|
||||
[InlineData(2)]
|
||||
[InlineData(1000)]
|
||||
public void ComputeDelay_CapBelowBase_NeverUndercutsTheBase(int failures)
|
||||
=> ConnectionBackoff.ComputeDelay(TimeSpan.FromSeconds(60), failures, Cap).ShouldBe(TimeSpan.FromSeconds(60));
|
||||
|
||||
/// <summary>A fresh throttle permits the first attempt immediately.</summary>
|
||||
[Fact]
|
||||
public void ShouldAttempt_FreshInstance_AllowsImmediately()
|
||||
|
||||
@@ -494,6 +494,38 @@ public sealed class PollGroupEngineTests
|
||||
readCount.ShouldBeLessThan(18);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// #999 / review K-10: with the backoff cap BELOW the subscription interval, a failing reader is polled no faster
|
||||
/// than a healthy one — every gap between attempts is at least the interval. Before the fix the cap was an
|
||||
/// absolute ceiling and the gap was the 50 ms cap.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public async Task Backoff_CapBelowInterval_NeverPollsAFailingDeviceFasterThanTheInterval()
|
||||
{
|
||||
var stamps = new ConcurrentQueue<long>();
|
||||
Task<IReadOnlyList<DataValueSnapshot>> Reader(IReadOnlyList<string> refs, CancellationToken ct)
|
||||
{
|
||||
stamps.Enqueue(System.Diagnostics.Stopwatch.GetTimestamp());
|
||||
throw new InvalidOperationException("dead device");
|
||||
}
|
||||
|
||||
await using var engine = new PollGroupEngine(
|
||||
Reader,
|
||||
(_, _, _) => { },
|
||||
minInterval: TimeSpan.FromMilliseconds(20),
|
||||
onError: _ => { },
|
||||
backoffCap: TimeSpan.FromMilliseconds(50));
|
||||
|
||||
var handle = engine.Subscribe(["X"], TimeSpan.FromMilliseconds(200));
|
||||
await WaitForAsync(() => stamps.Count >= 4, TimeSpan.FromSeconds(5));
|
||||
engine.Unsubscribe(handle);
|
||||
|
||||
var t = stamps.ToArray();
|
||||
for (var i = 1; i < 4; i++)
|
||||
System.Diagnostics.Stopwatch.GetElapsedTime(t[i - 1], t[i]).ShouldBeGreaterThanOrEqualTo(
|
||||
TimeSpan.FromMilliseconds(190), $"gap {i}");
|
||||
}
|
||||
|
||||
/// <summary>A successful poll resets the failure count so the cadence snaps back to the base interval.</summary>
|
||||
[Fact]
|
||||
public async Task Backoff_SuccessResetsToInterval()
|
||||
|
||||
Reference in New Issue
Block a user