fix(dashboard): apply reconnect delay when the snapshot watch completes cleanly (mxaccessgw#222)
Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -156,6 +156,15 @@ public sealed class DashboardSnapshotPublisher : BackgroundService
|
||||
_logger.LogWarning(ex, "Snapshot broadcast failed; will retry on the next snapshot tick.");
|
||||
}
|
||||
}
|
||||
|
||||
// The watch completed cleanly (or the host is stopping). Apply the same
|
||||
// reconnect delay as the fault path; without it a source that completes
|
||||
// immediately turns this loop into a hot spin.
|
||||
if (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
_logger.LogInformation("Snapshot subscription completed; reconnecting in {DelaySeconds:F1}s.", _reconnectDelay.TotalSeconds);
|
||||
await Task.Delay(_reconnectDelay, _timeProvider, stoppingToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
|
||||
@@ -68,13 +68,16 @@ public sealed class DashboardSnapshotPublisherTests
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sanity: a normal completion of WatchSnapshotsAsync (no exception)
|
||||
/// is re-subscribed; the publisher exits only on host shutdown.
|
||||
/// A clean completion of WatchSnapshotsAsync (no exception) is re-subscribed only after
|
||||
/// the reconnect delay; without it the publisher hot-spins re-opening a source that
|
||||
/// completes immediately. Driven by a fake clock, so nothing sleeps.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task ExecuteAsync_WhenSnapshotServiceCompletes_Resubscribes()
|
||||
public async Task ExecuteAsync_WhenSnapshotServiceCompletes_ResubscribesOnlyAfterReconnectDelay()
|
||||
{
|
||||
TimeSpan reconnectDelay = TimeSpan.FromMilliseconds(50);
|
||||
TickObservingTimeProvider clock = new() { WatchedDueTime = reconnectDelay };
|
||||
CompleteImmediatelySnapshotService snapshotService = new();
|
||||
RecordingHubContext hubContext = new();
|
||||
DashboardSnapshotPublisher publisher = new(
|
||||
@@ -82,19 +85,25 @@ public sealed class DashboardSnapshotPublisherTests
|
||||
hubContext,
|
||||
ConnectedCounter(),
|
||||
NullLogger<DashboardSnapshotPublisher>.Instance,
|
||||
TimeSpan.FromMilliseconds(50),
|
||||
reconnectDelay,
|
||||
IdlePollInterval,
|
||||
timeProvider: new FakeTimeProvider());
|
||||
timeProvider: clock);
|
||||
|
||||
using CancellationTokenSource cts = new();
|
||||
await publisher.StartAsync(cts.Token).WaitAsync(TestTimeout);
|
||||
|
||||
await snapshotService.Subscribes.WaitForAsync(1);
|
||||
await clock.WaitForWatchedTimersAsync(1, TestTimeout);
|
||||
|
||||
// Just short of the delay no re-subscribe may happen (a hot spin would have made many).
|
||||
clock.Advance(reconnectDelay - TimeSpan.FromMilliseconds(1));
|
||||
Assert.Equal(1, snapshotService.Subscribes.Count);
|
||||
|
||||
clock.Advance(TimeSpan.FromMilliseconds(1));
|
||||
await snapshotService.Subscribes.WaitForAsync(2);
|
||||
|
||||
await cts.CancelAsync();
|
||||
await publisher.StopAsync(CancellationToken.None);
|
||||
|
||||
Assert.True(snapshotService.Subscribes.Count >= 2);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
Reference in New Issue
Block a user