mbproxy: close out the dashboard code-review minor findings

Resolves the remaining Minor items from the 2026-05-15 review so the
web-UI dashboard work has no open follow-ups: a real-HubConnection
end-to-end test for the SignalR feed, stable mbproxy.admin.broadcast.*
log-event names, keyboard/aria accessibility on the fleet table,
frontend JS hardening (URL-decode guard, NaN guards, shared util.js),
reconciler<->capture-registry coverage, throwing-sink and embedded-asset
tests, broadcaster polish, and a soft upper bound on AdminPushIntervalMs.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Joseph Doherty
2026-05-16 16:36:39 -04:00
parent 374eecd205
commit 0308490aef
21 changed files with 576 additions and 67 deletions
+49 -7
View File
@@ -20,7 +20,7 @@ namespace Mbproxy.Admin;
/// SignalR host and all connections without firing per-connection disconnect cleanup
/// deterministically — never leaves a capture armed with no viewer.</para>
/// </summary>
internal sealed class StatusBroadcaster : IAsyncDisposable
internal sealed partial class StatusBroadcaster : IAsyncDisposable
{
private readonly IStatusPushSink _sink;
private readonly StatusSnapshotBuilder _builder;
@@ -32,6 +32,11 @@ internal sealed class StatusBroadcaster : IAsyncDisposable
private readonly CancellationTokenSource _cts = new();
private Task _loop = Task.CompletedTask;
// Guards StopAsync against a double-stop (DisposeAsync also calls StopAsync, and the
// owner may call StopAsync explicitly first) — symmetry with AdminEndpointHost's
// _disposed flag, and defends a future caller from touching the disposed CTS.
private bool _stopped;
public StatusBroadcaster(
IStatusPushSink sink,
StatusSnapshotBuilder builder,
@@ -56,6 +61,9 @@ internal sealed class StatusBroadcaster : IAsyncDisposable
/// </summary>
public async Task StopAsync()
{
if (_stopped) return;
_stopped = true;
if (!_cts.IsCancellationRequested)
await _cts.CancelAsync().ConfigureAwait(false);
@@ -81,7 +89,7 @@ internal sealed class StatusBroadcaster : IAsyncDisposable
}
catch (Exception ex)
{
_logger.LogError(ex, "StatusBroadcaster: failed to build status snapshot");
LogSnapshotFailed(_logger, ex);
return;
}
@@ -91,7 +99,7 @@ internal sealed class StatusBroadcaster : IAsyncDisposable
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
_logger.LogError(ex, "StatusBroadcaster: fleet push failed");
LogFleetPushFailed(_logger, ex);
}
// Reconcile capture arm state from the live viewer set. This is the single
@@ -100,18 +108,24 @@ internal sealed class StatusBroadcaster : IAsyncDisposable
var activePlcs = _tracker.ActivePlcs();
_captureRegistry.ReconcileArmed(activePlcs);
// Index the snapshot's PLC rows once per cycle — a per-active-PLC FirstOrDefault
// would be O(active × fleet).
Dictionary<string, PlcStatus>? plcsByName = activePlcs.Count > 0
? snapshot.Plcs.ToDictionary(p => p.Name, StringComparer.Ordinal)
: null;
foreach (var plcName in activePlcs)
{
try
{
var plc = snapshot.Plcs.FirstOrDefault(p => p.Name == plcName);
var plc = plcsByName!.GetValueOrDefault(plcName);
var debug = _builder.BuildDebug(plcName);
var detail = new PlcDetailResponse(plc, debug);
await _sink.PushPlcAsync(plcName, detail, ct).ConfigureAwait(false);
}
catch (Exception ex) when (ex is not OperationCanceledException)
{
_logger.LogError(ex, "StatusBroadcaster: detail push failed for PLC {Plc}", plcName);
LogDetailPushFailed(_logger, plcName, ex);
}
}
}
@@ -122,12 +136,15 @@ internal sealed class StatusBroadcaster : IAsyncDisposable
{
while (!ct.IsCancellationRequested)
{
// Push first, delay second — so a dashboard that connects right after the
// loop starts gets a snapshot immediately instead of waiting one interval.
await PushOnceAsync(ct).ConfigureAwait(false);
// Re-read the interval each cycle so an AdminPushIntervalMs hot-reload
// takes effect without restarting the loop. Floored at 100 ms to avoid a
// pathologically tight loop if a bad value slips past validation.
int interval = Math.Max(100, _options.CurrentValue.AdminPushIntervalMs);
await Task.Delay(interval, ct).ConfigureAwait(false);
await PushOnceAsync(ct).ConfigureAwait(false);
}
}
catch (OperationCanceledException)
@@ -136,7 +153,7 @@ internal sealed class StatusBroadcaster : IAsyncDisposable
}
catch (Exception ex)
{
_logger.LogError(ex, "StatusBroadcaster loop terminated unexpectedly");
LogLoopTerminated(_logger, ex);
}
}
@@ -145,4 +162,29 @@ internal sealed class StatusBroadcaster : IAsyncDisposable
await StopAsync().ConfigureAwait(false);
_cts.Dispose();
}
// ── Logging ──────────────────────────────────────────────────────────────
// Stable event names in the mbproxy.admin.broadcast.* family — see
// docs/Reference/LogEvents.md. EventIds continue the admin block (70/71 in
// AdminEndpointHost).
[LoggerMessage(EventId = 72, EventName = "mbproxy.admin.broadcast.snapshot.failed",
Level = LogLevel.Error,
Message = "Status broadcaster failed to build a status snapshot — this push cycle is skipped")]
private static partial void LogSnapshotFailed(ILogger logger, Exception ex);
[LoggerMessage(EventId = 73, EventName = "mbproxy.admin.broadcast.fleet.failed",
Level = LogLevel.Error,
Message = "Status broadcaster failed to push the fleet snapshot to dashboard subscribers")]
private static partial void LogFleetPushFailed(ILogger logger, Exception ex);
[LoggerMessage(EventId = 74, EventName = "mbproxy.admin.broadcast.detail.failed",
Level = LogLevel.Error,
Message = "Status broadcaster failed to push the detail snapshot for PLC {Plc}")]
private static partial void LogDetailPushFailed(ILogger logger, string plc, Exception ex);
[LoggerMessage(EventId = 75, EventName = "mbproxy.admin.broadcast.loop.terminated",
Level = LogLevel.Error,
Message = "Status broadcaster push loop terminated unexpectedly — the live dashboard feed has stopped")]
private static partial void LogLoopTerminated(ILogger logger, Exception ex);
}