diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/ConnectionBackoff.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/ConnectionBackoff.cs index 3a2f49ff2..3f384d98a 100644 --- a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/ConnectionBackoff.cs +++ b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/ConnectionBackoff.cs @@ -42,7 +42,8 @@ public sealed class ConnectionBackoff /// /// Capped exponential backoff schedule. ≤ 0 returns /// ; each subsequent failure doubles the wait up to - /// . Computed in ticks with an overflow guard so a large failure + /// — or up to 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 ComputeBackoffDelay. /// @@ -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); diff --git a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/PollGroupEngine.cs b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/PollGroupEngine.cs index 704b8eadd..3f163a387 100644 --- a/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/PollGroupEngine.cs +++ b/src/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions/PollGroupEngine.cs @@ -107,11 +107,11 @@ public sealed class PollGroupEngine : IAsyncDisposable /// The interval feeding that schedule is the per-subscription interval /// already clamped to in , so /// the first failure's wait equals the clamped interval (shift 0 → 1×) and the floor is - /// honoured for free. One asymmetry worth knowing: ComputeDelay saturates at the cap - /// unconditionally, so a set SMALLER than the clamped - /// interval makes failures retry FASTER than the interval floor — the cap outranks - /// on the failure branch. Configure it above the floor - /// (every driver here uses 30 s against a 100 ms floor). + /// honoured for free. A SMALLER than a subscription's clamped + /// interval never shortens it (#999 / review K-10): ComputeDelay 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.) /// 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 callbacks. The diff --git a/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests/ConnectionBackoffTests.cs b/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests/ConnectionBackoffTests.cs index 3af88cf09..2c45e2c1c 100644 --- a/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests/ConnectionBackoffTests.cs +++ b/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests/ConnectionBackoffTests.cs @@ -42,6 +42,18 @@ public sealed class ConnectionBackoffTests public void ComputeDelay_SaturatesAtCap(int failures) => ConnectionBackoff.ComputeDelay(Base, failures, Cap).ShouldBe(Cap); + /// + /// #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. + /// + [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)); + /// A fresh throttle permits the first attempt immediately. [Fact] public void ShouldAttempt_FreshInstance_AllowsImmediately() diff --git a/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests/PollGroupEngineTests.cs b/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests/PollGroupEngineTests.cs index fdd7ed592..942a8801b 100644 --- a/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests/PollGroupEngineTests.cs +++ b/tests/Core/ZB.MOM.WW.OtOpcUa.Core.Abstractions.Tests/PollGroupEngineTests.cs @@ -494,6 +494,38 @@ public sealed class PollGroupEngineTests readCount.ShouldBeLessThan(18); } + /// + /// #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. + /// + [Fact] + public async Task Backoff_CapBelowInterval_NeverPollsAFailingDeviceFasterThanTheInterval() + { + var stamps = new ConcurrentQueue(); + Task> Reader(IReadOnlyList 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}"); + } + /// A successful poll resets the failure count so the cadence snaps back to the base interval. [Fact] public async Task Backoff_SuccessResetsToInterval()