fix(sessions): renew the lease on last-subscriber detach; purge item registrations on Unregister (SES-004, SES-007, #253)
This commit is contained in:
@@ -1697,6 +1697,13 @@ public sealed class GatewaySession : IAsyncDisposable
|
||||
case MxCommandKind.UnsubscribeBulk:
|
||||
RemoveItems(command.UnsubscribeBulk.ServerHandle, command.UnsubscribeBulk.ItemHandles);
|
||||
break;
|
||||
case MxCommandKind.Unregister:
|
||||
// Unregister invalidates every item under the server handle in MXAccess, so
|
||||
// keeping their registrations would grow _items on each register/unregister
|
||||
// cycle and leave stale tag addresses for the constraint checks to read if
|
||||
// MXAccess hands the same handle values out again.
|
||||
RemoveServerItems(command.Unregister.ServerHandle);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2630,6 +2637,28 @@ public sealed class GatewaySession : IAsyncDisposable
|
||||
}
|
||||
}
|
||||
|
||||
private void RemoveServerItems(int serverHandle)
|
||||
{
|
||||
List<(int ServerHandle, int ItemHandle)>? stale = null;
|
||||
foreach ((int ServerHandle, int ItemHandle) key in _items.Keys)
|
||||
{
|
||||
if (key.ServerHandle == serverHandle)
|
||||
{
|
||||
(stale ??= []).Add(key);
|
||||
}
|
||||
}
|
||||
|
||||
if (stale is null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ((int ServerHandle, int ItemHandle) key in stale)
|
||||
{
|
||||
_items.Remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
private void DetachEventSubscriber()
|
||||
{
|
||||
lock (_syncRoot)
|
||||
@@ -2659,11 +2688,23 @@ public sealed class GatewaySession : IAsyncDisposable
|
||||
// before any subscriber registered; that never-subscribed session must not enter the
|
||||
// grace window.
|
||||
if (_everHadEventSubscriber
|
||||
&& _detachGrace > TimeSpan.Zero
|
||||
&& _activeEventSubscriberCount == 0
|
||||
&& _state is not (SessionState.Closing or SessionState.Closed or SessionState.Faulted))
|
||||
{
|
||||
_detachedAtUtc = _eventStreaming.TimeProvider.GetUtcNow();
|
||||
DateTimeOffset now = _eventStreaming.TimeProvider.GetUtcNow();
|
||||
|
||||
// Streaming is client activity, and the detach is its last moment. The lease only
|
||||
// moves on unary commands, so a client that streamed for longer than the lease with
|
||||
// no unary call would otherwise detach with a lease already in the past — and the
|
||||
// next sweep would close it as lease-expired (which takes precedence) before the
|
||||
// detach-grace window it is entitled to. Renewing here keeps the reconnect window.
|
||||
_lastClientActivityAt = now;
|
||||
_leaseExpiresAt = now + LeaseDuration;
|
||||
|
||||
if (_detachGrace > TimeSpan.Zero)
|
||||
{
|
||||
_detachedAtUtc = now;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user