5d4aefc99c
The 256-tag LRU was global, so two Browse circuits on different areas evicted each other's whole sets every poll. Tags now record the viewer that read them last; each active viewer (read within 30s) owns MaxSubscribedTags/active (floor 32). Eviction takes other viewers' excess over their share first (inactive viewers' share is zero), then the reader's own untouched tags, and never strips a neighbour within its share (soft cap). Single-viewer behaviour is unchanged. IDashboardLiveDataService gains a viewerId ReadAsync overload.
593 lines
24 KiB
C#
593 lines
24 KiB
C#
using Microsoft.Extensions.Options;
|
|
using ZB.MOM.WW.MxGateway.Contracts.Proto;
|
|
using ZB.MOM.WW.MxGateway.Server.Alarms;
|
|
using ZB.MOM.WW.MxGateway.Server.Configuration;
|
|
using ZB.MOM.WW.MxGateway.Server.Sessions;
|
|
|
|
namespace ZB.MOM.WW.MxGateway.Server.Dashboard;
|
|
|
|
/// <summary>
|
|
/// Default <see cref="IDashboardLiveDataService"/>. Owns one shared gateway
|
|
/// session for the whole dashboard: it is opened lazily on first use and
|
|
/// re-opened transparently whenever it faults, is closed, or its lease
|
|
/// expires. All access is serialised through <see cref="_gate"/> so the
|
|
/// single backing worker only ever sees one in-flight command.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This service is also where <c>MxGateway:Dashboard:ShowTagValues</c> is
|
|
/// applied to the Browse panel: with the flag false (the default) the
|
|
/// formatted value never leaves this boundary — the projected
|
|
/// <see cref="DashboardTagValue"/> carries
|
|
/// <see cref="DashboardTagValue.RedactedValueText"/> instead. Putting the
|
|
/// decision at the service rather than in the page keeps it to one place and
|
|
/// keeps a value out of the render tree entirely, rather than relying on
|
|
/// every current and future view to remember to suppress it.
|
|
/// </remarks>
|
|
public sealed class DashboardLiveDataService : IDashboardLiveDataService, IAsyncDisposable
|
|
{
|
|
private const string BackendName = "Galaxy";
|
|
private const string ClientName = "mxgateway-dashboard";
|
|
|
|
// One browse page of tags plus headroom. Bounds the standing advise load the
|
|
// single dashboard worker carries — and the event churn that advise set feeds —
|
|
// however much of a galaxy an operator browses through in one sitting.
|
|
//
|
|
// The bound is per-read, not absolute: a read may never evict a tag it is itself
|
|
// about to return, so a single read of more distinct tags than the cap leaves the
|
|
// set that large. With a single viewer the invariant EvictForAsync maintains is
|
|
//
|
|
// |advise set| after a read <= max(MaxSubscribedTags, distinct tags in that read)
|
|
//
|
|
// (with several active viewers the per-viewer share below can hold it higher, by
|
|
// at most what the other viewers hold within their shares) and any overshoot is squeezed back out by the next read that subscribes a tag
|
|
// (see EvictForAsync). A browse page requests far fewer tags than the cap, so in
|
|
// practice the set settles at MaxSubscribedTags.
|
|
private const int MaxSubscribedTags = 256;
|
|
|
|
// Per-viewer soft cap: MaxSubscribedTags is shared by every active viewer (Browse
|
|
// circuit), so each owns at most MaxSubscribedTags / activeViewers tags before its
|
|
// own least-recently-read tags become the first eviction candidates — never fewer
|
|
// than this floor. Without the split, two circuits browsing different areas
|
|
// thrashed the single global LRU: each read evicted the other circuit's whole set,
|
|
// which then re-advised it two seconds later.
|
|
private const int MinTagsPerViewer = 32;
|
|
|
|
// A viewer that has not read within this window no longer counts as active: its
|
|
// share is released and its tags become the first eviction candidates. Generous
|
|
// against the Browse page's 2 s poll, so a briefly paused circuit keeps its share.
|
|
private static readonly TimeSpan ActiveViewerWindow = TimeSpan.FromSeconds(30);
|
|
|
|
// Viewer id for callers that do not identify themselves.
|
|
private const string SharedViewerId = "";
|
|
|
|
private static readonly TimeSpan ReadTimeout = TimeSpan.FromSeconds(5);
|
|
|
|
private readonly ISessionManager _sessionManager;
|
|
private readonly IGatewayAlarmService _alarmService;
|
|
private readonly ILogger<DashboardLiveDataService> _logger;
|
|
private readonly TimeProvider _timeProvider;
|
|
private readonly SemaphoreSlim _gate = new(1, 1);
|
|
|
|
/// <summary>
|
|
/// <c>MxGateway:Dashboard:ShowTagValues</c>. False (the default)
|
|
/// substitutes <see cref="DashboardTagValue.RedactedValueText"/> for every
|
|
/// successfully read value this service hands the Browse panel; quality,
|
|
/// data type, source timestamp, and any error still describe the real
|
|
/// read, so the panel remains a diagnostic surface without being a
|
|
/// value-disclosure one. Failed reads keep their existing "-" placeholder:
|
|
/// there was no value to suppress, so claiming one was withheld would
|
|
/// misreport the failure.
|
|
/// </summary>
|
|
private readonly bool _showTagValues;
|
|
|
|
// Least-recently-read-last advise set: the list holds every currently advised
|
|
// tag ordered most- to least-recently read, the dictionary indexes into it.
|
|
// Both are only ever touched under _gate, which already serialises all viewers.
|
|
private readonly Dictionary<string, LinkedListNode<SubscribedTag>> _subscribed =
|
|
new(StringComparer.OrdinalIgnoreCase);
|
|
|
|
private readonly LinkedList<SubscribedTag> _recency = new();
|
|
|
|
// Per-viewer ownership and activity. A tag is owned by the viewer that read it
|
|
// last; only touched under _gate.
|
|
private readonly Dictionary<string, ViewerState> _viewers = new(StringComparer.Ordinal);
|
|
|
|
private GatewaySession? _session;
|
|
private int _serverHandle;
|
|
private bool _disposed;
|
|
|
|
/// <summary>Initializes the live-data service.</summary>
|
|
/// <param name="sessionManager">Gateway session manager.</param>
|
|
/// <param name="alarmService">Gateway central alarm service.</param>
|
|
/// <param name="options">Gateway options supplying <c>Dashboard:ShowTagValues</c>.</param>
|
|
/// <param name="logger">Diagnostic logger.</param>
|
|
/// <param name="timeProvider">Clock for viewer activity; defaults to system time.</param>
|
|
public DashboardLiveDataService(
|
|
ISessionManager sessionManager,
|
|
IGatewayAlarmService alarmService,
|
|
IOptions<GatewayOptions> options,
|
|
ILogger<DashboardLiveDataService> logger,
|
|
TimeProvider? timeProvider = null)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(options);
|
|
_sessionManager = sessionManager ?? throw new ArgumentNullException(nameof(sessionManager));
|
|
_alarmService = alarmService ?? throw new ArgumentNullException(nameof(alarmService));
|
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
|
_showTagValues = options.Value.Dashboard.ShowTagValues;
|
|
_timeProvider = timeProvider ?? TimeProvider.System;
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task<DashboardLiveReadResult> ReadAsync(
|
|
IReadOnlyCollection<string> tagAddresses,
|
|
CancellationToken cancellationToken) => ReadAsync(tagAddresses, viewerId: null, cancellationToken);
|
|
|
|
/// <inheritdoc />
|
|
public async Task<DashboardLiveReadResult> ReadAsync(
|
|
IReadOnlyCollection<string> tagAddresses,
|
|
string? viewerId,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(tagAddresses);
|
|
if (tagAddresses.Count == 0)
|
|
{
|
|
return DashboardLiveReadResult.Empty;
|
|
}
|
|
|
|
await _gate.WaitAsync(cancellationToken).ConfigureAwait(false);
|
|
try
|
|
{
|
|
(GatewaySession session, int serverHandle) = await EnsureReadyAsync(cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
string viewer = viewerId ?? SharedViewerId;
|
|
DateTimeOffset now = _timeProvider.GetUtcNow();
|
|
TouchViewer(viewer, now);
|
|
|
|
string[] toSubscribe = TouchAndCollectNewTags(tagAddresses, viewer, out HashSet<string> justRead);
|
|
if (toSubscribe.Length > 0)
|
|
{
|
|
await EvictForAsync(session, serverHandle, toSubscribe.Length, justRead, viewer, now, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
IReadOnlyList<SubscribeResult> subscribeResults = await session
|
|
.SubscribeBulkAsync(serverHandle, toSubscribe, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
TrackSubscribed(toSubscribe, subscribeResults, viewer);
|
|
}
|
|
|
|
IReadOnlyList<BulkReadResult> results = await session
|
|
.ReadBulkAsync(serverHandle, tagAddresses.ToArray(), ReadTimeout, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
// The only place the /browse live-value gate is evaluated: the page
|
|
// renders whatever ValueText it is handed, so a second check in the
|
|
// view could only ever disagree with this one.
|
|
//
|
|
// A failed read is left alone: it has no value to suppress, and its
|
|
// ValueText is already the "-" placeholder. Substituting "[redacted]"
|
|
// there would tell the operator a value was withheld when the read
|
|
// never produced one — the Error column says why it failed, and the
|
|
// two must not contradict each other.
|
|
DashboardTagValue[] values = results
|
|
.Select(DashboardTagValue.FromBulkReadResult)
|
|
.Select(value => _showTagValues || !value.Ok
|
|
? value
|
|
: value with { ValueText = DashboardTagValue.RedactedValueText })
|
|
.ToArray();
|
|
return new DashboardLiveReadResult(values, null, session.SessionId, session.WorkerProcessId);
|
|
}
|
|
catch (Exception exception) when (exception is not OperationCanceledException)
|
|
{
|
|
InvalidateSession();
|
|
_logger.LogWarning(exception, "Dashboard live read failed; the dashboard session will be re-opened.");
|
|
return new DashboardLiveReadResult([], exception.Message, null, null);
|
|
}
|
|
finally
|
|
{
|
|
_gate.Release();
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public Task<DashboardAlarmQueryResult> QueryAlarmsAsync(CancellationToken cancellationToken)
|
|
{
|
|
// Alarms come from the gateway's always-on central monitor; the
|
|
// dashboard reads its in-process cache directly — no session needed.
|
|
DashboardActiveAlarm[] alarms = _alarmService.CurrentAlarms
|
|
.Select(DashboardActiveAlarm.FromSnapshot)
|
|
.ToArray();
|
|
|
|
string? error = _alarmService.State is GatewayAlarmMonitorState.Monitoring
|
|
or GatewayAlarmMonitorState.Disabled
|
|
? null
|
|
: _alarmService.LastError ?? $"Alarm monitor is {_alarmService.State}.";
|
|
|
|
return Task.FromResult(new DashboardAlarmQueryResult(
|
|
alarms,
|
|
error,
|
|
_alarmService.WorkerProcessId,
|
|
_alarmService.SnapshotTruncated));
|
|
}
|
|
|
|
// Promotes every already-advised tag in this read to the front of the recency
|
|
// list, hands its ownership to the reading viewer, and returns the tags that
|
|
// still need subscribing (distinct, in request order). `justRead` is every
|
|
// distinct tag of this read — none of them may be evicted to make room for the
|
|
// same read's new tags. Callers must hold _gate.
|
|
//
|
|
// Every tag of one read is equally recently read; the recency list needs a total
|
|
// order anyway, so the whole service uses one tie-break: later in the request wins.
|
|
// Promoting in request order gives that here, and TrackSubscribed inserts new tags
|
|
// the same way.
|
|
private string[] TouchAndCollectNewTags(
|
|
IReadOnlyCollection<string> tagAddresses,
|
|
string viewer,
|
|
out HashSet<string> justRead)
|
|
{
|
|
List<string> toSubscribe = [];
|
|
HashSet<string> seen = new(StringComparer.OrdinalIgnoreCase);
|
|
|
|
foreach (string tag in tagAddresses)
|
|
{
|
|
if (_subscribed.TryGetValue(tag, out LinkedListNode<SubscribedTag>? node))
|
|
{
|
|
if (!ReferenceEquals(node, _recency.First))
|
|
{
|
|
_recency.Remove(node);
|
|
_recency.AddFirst(node);
|
|
}
|
|
|
|
if (!string.Equals(node.Value.Owner, viewer, StringComparison.Ordinal))
|
|
{
|
|
AdjustOwned(node.Value.Owner, -1);
|
|
AdjustOwned(viewer, +1);
|
|
node.Value = node.Value with { Owner = viewer };
|
|
}
|
|
|
|
seen.Add(tag);
|
|
}
|
|
else if (seen.Add(tag))
|
|
{
|
|
toSubscribe.Add(tag);
|
|
}
|
|
}
|
|
|
|
justRead = seen;
|
|
return [.. toSubscribe];
|
|
}
|
|
|
|
// Drops least-recently-read tags off the back of the advise set until the
|
|
// incoming tags fit under MaxSubscribedTags, unadvising them on the worker in
|
|
// one batch. A failed unadvise must not fail the read: the tags are dropped
|
|
// from tracking regardless, and the session-invalidation path already handles
|
|
// gateway/worker drift. Callers must hold _gate.
|
|
//
|
|
// Victims are chosen in two passes, each least-recently-read first:
|
|
// 1. tags of OTHER viewers that are over their share (PerViewerCap; an
|
|
// inactive viewer's share is zero);
|
|
// 2. the reading viewer's own tags that this read did not touch.
|
|
// A viewer within its share is never evicted by someone else's read, so the
|
|
// cap is soft: when every other viewer sits within its share and the reader's
|
|
// own older tags run out, the set ends over MaxSubscribedTags rather than
|
|
// stripping a neighbour. With one viewer, pass 1 is empty and pass 2 is the
|
|
// plain global LRU.
|
|
//
|
|
// Eviction never touches the tags this read just touched (`justRead`), so a read
|
|
// whose own distinct tags outnumber the cap ends over it — see MaxSubscribedTags
|
|
// for the exact invariant. That overshoot is not sticky: the next read that
|
|
// subscribes anything computes `overflow` against the oversized set and evicts the
|
|
// whole excess in one pass (a 300-tag set plus one new tag evicts 45 and lands
|
|
// back at the cap). A read that subscribes nothing new evicts nothing, but it also
|
|
// cannot grow the set.
|
|
//
|
|
// Cancellation mid-eviction follows this file's policy: OperationCanceledException
|
|
// is deliberately not caught here or in ReadAsync, so it propagates with the tags
|
|
// already dropped from tracking — the same end state as a failed unadvise.
|
|
private async Task EvictForAsync(
|
|
GatewaySession session,
|
|
int serverHandle,
|
|
int incomingCount,
|
|
HashSet<string> justRead,
|
|
string reader,
|
|
DateTimeOffset now,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
int overflow = _subscribed.Count + incomingCount - MaxSubscribedTags;
|
|
if (overflow <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
int share = PerViewerCap(now);
|
|
List<int> evictedHandles = [];
|
|
int evictCount = 0;
|
|
|
|
// Pass 1: other viewers over their share.
|
|
for (LinkedListNode<SubscribedTag>? node = _recency.Last; node is not null && overflow > 0;)
|
|
{
|
|
LinkedListNode<SubscribedTag>? previous = node.Previous;
|
|
string owner = node.Value.Owner;
|
|
if (!string.Equals(owner, reader, StringComparison.Ordinal)
|
|
&& !justRead.Contains(node.Value.TagAddress)
|
|
&& OwnedCount(owner) > (IsActive(owner, now) ? share : 0))
|
|
{
|
|
Evict(node, evictedHandles);
|
|
overflow--;
|
|
evictCount++;
|
|
}
|
|
|
|
node = previous;
|
|
}
|
|
|
|
// Pass 2: the reader's own tags this read did not touch.
|
|
for (LinkedListNode<SubscribedTag>? node = _recency.Last; node is not null && overflow > 0;)
|
|
{
|
|
LinkedListNode<SubscribedTag>? previous = node.Previous;
|
|
if (string.Equals(node.Value.Owner, reader, StringComparison.Ordinal)
|
|
&& !justRead.Contains(node.Value.TagAddress))
|
|
{
|
|
Evict(node, evictedHandles);
|
|
overflow--;
|
|
evictCount++;
|
|
}
|
|
|
|
node = previous;
|
|
}
|
|
|
|
if (evictCount == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_logger.LogDebug(
|
|
"Dashboard advise set hit its cap of {Cap}; evicted {EvictedCount} least-recently-read tags.",
|
|
MaxSubscribedTags,
|
|
evictCount);
|
|
|
|
if (evictedHandles.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
await session.UnsubscribeBulkAsync(serverHandle, evictedHandles, cancellationToken)
|
|
.ConfigureAwait(false);
|
|
}
|
|
catch (Exception exception) when (exception is not OperationCanceledException)
|
|
{
|
|
_logger.LogDebug(
|
|
exception,
|
|
"Unadvising {EvictedCount} evicted dashboard tags failed; they stay dropped from tracking.",
|
|
evictedHandles.Count);
|
|
}
|
|
}
|
|
|
|
// Removes one tag from tracking, releasing its owner's count and collecting its
|
|
// item handle for the batch unadvise. Callers must hold _gate.
|
|
private void Evict(LinkedListNode<SubscribedTag> node, List<int> evictedHandles)
|
|
{
|
|
_recency.Remove(node);
|
|
_subscribed.Remove(node.Value.TagAddress);
|
|
AdjustOwned(node.Value.Owner, -1);
|
|
if (node.Value.ItemHandle != 0)
|
|
{
|
|
evictedHandles.Add(node.Value.ItemHandle);
|
|
}
|
|
}
|
|
|
|
// Each active viewer's share of MaxSubscribedTags, floored at MinTagsPerViewer.
|
|
// Callers must hold _gate.
|
|
private int PerViewerCap(DateTimeOffset now)
|
|
{
|
|
int active = _viewers.Values.Count(state => now - state.LastReadAt <= ActiveViewerWindow);
|
|
return Math.Max(MinTagsPerViewer, MaxSubscribedTags / Math.Max(1, active));
|
|
}
|
|
|
|
private bool IsActive(string viewer, DateTimeOffset now) =>
|
|
_viewers.TryGetValue(viewer, out ViewerState? state) && now - state.LastReadAt <= ActiveViewerWindow;
|
|
|
|
private int OwnedCount(string viewer) =>
|
|
_viewers.TryGetValue(viewer, out ViewerState? state) ? state.OwnedCount : 0;
|
|
|
|
// Records a read by `viewer` and forgets viewers that are both inactive and own
|
|
// nothing, so the map does not grow one entry per Browse page ever opened.
|
|
// Callers must hold _gate.
|
|
private void TouchViewer(string viewer, DateTimeOffset now)
|
|
{
|
|
if (!_viewers.TryGetValue(viewer, out ViewerState? state))
|
|
{
|
|
state = new ViewerState();
|
|
_viewers[viewer] = state;
|
|
}
|
|
|
|
state.LastReadAt = now;
|
|
|
|
foreach (KeyValuePair<string, ViewerState> entry in _viewers.ToArray())
|
|
{
|
|
if (entry.Value.OwnedCount <= 0 && now - entry.Value.LastReadAt > ActiveViewerWindow)
|
|
{
|
|
_viewers.Remove(entry.Key);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Callers must hold _gate.
|
|
private void AdjustOwned(string viewer, int delta)
|
|
{
|
|
if (!_viewers.TryGetValue(viewer, out ViewerState? state))
|
|
{
|
|
if (delta <= 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
state = new ViewerState { LastReadAt = DateTimeOffset.MinValue };
|
|
_viewers[viewer] = state;
|
|
}
|
|
|
|
state.OwnedCount = Math.Max(0, state.OwnedCount + delta);
|
|
}
|
|
|
|
// Records the freshly advised tags as the most recently read, keeping each
|
|
// tag's item handle so eviction can unadvise it. Tags the worker failed to
|
|
// advise are still tracked (matching the pre-cap behaviour of not retrying
|
|
// them on every read) but carry no handle, so eviction just forgets them.
|
|
// Callers must hold _gate.
|
|
private void TrackSubscribed(
|
|
IReadOnlyList<string> tagAddresses,
|
|
IReadOnlyList<SubscribeResult> results,
|
|
string viewer)
|
|
{
|
|
Dictionary<string, int> handles = new(results.Count, StringComparer.OrdinalIgnoreCase);
|
|
foreach (SubscribeResult result in results)
|
|
{
|
|
if (result.WasSuccessful && !string.IsNullOrEmpty(result.TagAddress))
|
|
{
|
|
handles[result.TagAddress] = result.ItemHandle;
|
|
}
|
|
}
|
|
|
|
// Request order, so the read's last tag ends up most recent — the same
|
|
// tie-break TouchAndCollectNewTags applies to the tags it promotes.
|
|
foreach (string tag in tagAddresses)
|
|
{
|
|
handles.TryGetValue(tag, out int itemHandle);
|
|
_subscribed[tag] = _recency.AddFirst(new SubscribedTag(tag, itemHandle, viewer));
|
|
AdjustOwned(viewer, +1);
|
|
}
|
|
}
|
|
|
|
// Forgets the whole advise set without unadvising: every call site is one where
|
|
// the backing session (and with it every item handle) is already gone.
|
|
// Callers must hold _gate.
|
|
private void ClearSubscriptions()
|
|
{
|
|
_subscribed.Clear();
|
|
_recency.Clear();
|
|
foreach (ViewerState state in _viewers.Values)
|
|
{
|
|
state.OwnedCount = 0;
|
|
}
|
|
}
|
|
|
|
// Returns a Ready session + its Register server handle, opening a fresh
|
|
// session when none exists or the current one is no longer usable. Callers
|
|
// must hold _gate.
|
|
private async Task<(GatewaySession Session, int ServerHandle)> EnsureReadyAsync(
|
|
CancellationToken cancellationToken)
|
|
{
|
|
ObjectDisposedException.ThrowIf(_disposed, this);
|
|
|
|
GatewaySession? existing = _session;
|
|
if (existing is not null
|
|
&& existing.State == SessionState.Ready
|
|
&& _sessionManager.TryGetSession(existing.SessionId, out _))
|
|
{
|
|
return (existing, _serverHandle);
|
|
}
|
|
|
|
if (existing is not null)
|
|
{
|
|
_logger.LogInformation(
|
|
"Dashboard session {SessionId} is no longer usable (state {State}); re-opening.",
|
|
existing.SessionId,
|
|
existing.State);
|
|
await CloseQuietlyAsync(existing.SessionId).ConfigureAwait(false);
|
|
}
|
|
|
|
ClearSubscriptions();
|
|
_session = null;
|
|
|
|
GatewaySession session = await _sessionManager.OpenSessionAsync(
|
|
new SessionOpenRequest(BackendName, ClientName, Guid.NewGuid().ToString("N"), CommandTimeout: null),
|
|
ClientName,
|
|
ownerKeyId: null,
|
|
cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
WorkerCommandReply reply = await session.InvokeAsync(
|
|
new WorkerCommand
|
|
{
|
|
Command = new MxCommand
|
|
{
|
|
Kind = MxCommandKind.Register,
|
|
Register = new RegisterCommand { ClientName = ClientName },
|
|
},
|
|
},
|
|
cancellationToken)
|
|
.ConfigureAwait(false);
|
|
|
|
int? serverHandle = reply.Reply?.Register?.ServerHandle;
|
|
if (serverHandle is null)
|
|
{
|
|
string diagnostic = reply.Reply?.ProtocolStatus?.Message
|
|
?? reply.Reply?.DiagnosticMessage
|
|
?? "Worker did not return a server handle for Register.";
|
|
await CloseQuietlyAsync(session.SessionId).ConfigureAwait(false);
|
|
throw new InvalidOperationException($"Dashboard session registration failed: {diagnostic}");
|
|
}
|
|
|
|
_session = session;
|
|
_serverHandle = serverHandle.Value;
|
|
_logger.LogInformation(
|
|
"Dashboard session {SessionId} opened (worker pid {WorkerPid}).",
|
|
session.SessionId,
|
|
session.WorkerProcessId);
|
|
return (session, _serverHandle);
|
|
}
|
|
|
|
// Drops the cached session so the next call re-opens. Callers must hold _gate.
|
|
private void InvalidateSession()
|
|
{
|
|
_session = null;
|
|
_serverHandle = 0;
|
|
ClearSubscriptions();
|
|
}
|
|
|
|
private async Task CloseQuietlyAsync(string sessionId)
|
|
{
|
|
try
|
|
{
|
|
await _sessionManager.CloseSessionAsync(sessionId, CancellationToken.None).ConfigureAwait(false);
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
_logger.LogDebug(exception, "Closing stale dashboard session {SessionId} failed.", sessionId);
|
|
}
|
|
}
|
|
|
|
/// <summary>Closes the underlying gateway session, if one is open.</summary>
|
|
/// <returns>A task that represents the asynchronous operation.</returns>
|
|
public async ValueTask DisposeAsync()
|
|
{
|
|
if (_disposed)
|
|
{
|
|
return;
|
|
}
|
|
|
|
_disposed = true;
|
|
GatewaySession? session = _session;
|
|
_session = null;
|
|
if (session is not null)
|
|
{
|
|
await CloseQuietlyAsync(session.SessionId).ConfigureAwait(false);
|
|
}
|
|
|
|
_gate.Dispose();
|
|
}
|
|
|
|
// One entry of the advise set. ItemHandle is the handle the worker bound for
|
|
// the tag, or 0 when the subscribe failed and there is nothing to unadvise.
|
|
// Owner is the viewer that read the tag most recently.
|
|
private readonly record struct SubscribedTag(string TagAddress, int ItemHandle, string Owner);
|
|
|
|
// One viewer's slice of the advise set.
|
|
private sealed class ViewerState
|
|
{
|
|
public int OwnedCount { get; set; }
|
|
|
|
public DateTimeOffset LastReadAt { get; set; }
|
|
}
|
|
}
|