perf(gateway): attach the dashboard mirror only while a viewer watches (#235)

Every session ran a dashboard-mirror distributor subscriber that woke for every
event only for Publish to discard it, since normally nobody has a
session-details page open. The viewer registry now raises
ViewerPresenceChanged on a session's first viewer and last viewer; the
production broadcaster exposes it as IDashboardViewerPresence, and a session
attaches its mirror while watched and disposes the lease when not.

MarkReady still starts the pump, so draining, replay retention, the alarm lease
and dead-worker detection are unchanged. ACL and value redaction are untouched.
Broadcasters without presence (test doubles) keep the always-on mirror.

Internal distributor subscribers can now pass a filter applied before the
pump's TryWrite; the alarm monitor admits only alarm transitions and provider
mode changes, so data changes no longer wake it.
This commit is contained in:
Joseph Doherty
2026-11-21 08:18:26 -05:00
parent 5516561084
commit 9b9fdecc9b
10 changed files with 543 additions and 77 deletions
@@ -363,12 +363,19 @@ public sealed class SessionEventDistributor : IAsyncDisposable
/// disconnected from the mirror. Defaults to <see langword="false"/> (external
/// subscriber) so every existing call site is unchanged.
/// </param>
/// <param name="filter">
/// Optional predicate the pump applies before writing an event to this subscriber; an
/// event it rejects is skipped for this subscriber only, so it neither wakes the reader
/// nor occupies channel capacity. It runs on the pump thread — normally the worker pipe
/// read loop — for every event, so it must be cheap, pure, and must not throw.
/// <see langword="null"/> (the default) delivers every event.
/// </param>
/// <returns>The lease for the newly-registered subscriber.</returns>
public IEventSubscriberLease Register(bool isInternal = false)
public IEventSubscriberLease Register(bool isInternal = false, Func<MxEvent, bool>? filter = null)
{
Channel<MxEvent> channel = CreateSubscriberChannel();
long id = Interlocked.Increment(ref _nextSubscriberId);
Subscriber subscriber = new(id, channel, isInternal);
Subscriber subscriber = new(id, channel, isInternal, filter);
return RegisterSubscriber(subscriber);
}
@@ -517,7 +524,7 @@ public sealed class SessionEventDistributor : IAsyncDisposable
{
Channel<MxEvent> channel = CreateSubscriberChannel();
long id = Interlocked.Increment(ref _nextSubscriberId);
Subscriber subscriber = new(id, channel, isInternal);
Subscriber subscriber = new(id, channel, isInternal, filter: null);
// Snapshot replay AND register under a single _replayLock section so the live channel
// begins exactly where the replay snapshot ends — see the remarks for the no-gap /
@@ -680,6 +687,11 @@ public sealed class SessionEventDistributor : IAsyncDisposable
// THIS subscriber only; the pump, the session, and every other subscriber
// keep running. Logs identifiers (worker sequence, subscriber id, session)
// only, never the event payload or tag values.
if (subscriber.Filter is { } filter && !filter(mxEvent))
{
continue;
}
if (!subscriber.Channel.Writer.TryWrite(mxEvent))
{
OnSubscriberOverflow(subscriber, mxEvent.WorkerSequence);
@@ -1035,7 +1047,7 @@ public sealed class SessionEventDistributor : IAsyncDisposable
private readonly record struct ReplayEntry(MxEvent Event, DateTimeOffset RetainedAt, int SizeBytes);
private sealed class Subscriber(long id, Channel<MxEvent> channel, bool isInternal)
private sealed class Subscriber(long id, Channel<MxEvent> channel, bool isInternal, Func<MxEvent, bool>? filter)
{
/// <summary>Gets the subscriber's monotonic id, assigned at registration.</summary>
public long Id { get; } = id;
@@ -1049,6 +1061,12 @@ public sealed class SessionEventDistributor : IAsyncDisposable
/// fault the session.
/// </summary>
public bool IsInternal { get; } = isInternal;
/// <summary>
/// Gets the optional per-subscriber predicate; an event it rejects is not written to
/// this subscriber. <see langword="null"/> delivers every event.
/// </summary>
public Func<MxEvent, bool>? Filter { get; } = filter;
}
private sealed class SubscriberLease(SessionEventDistributor distributor, Subscriber subscriber)