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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -595,6 +595,93 @@ public sealed class GatewaySessionTests
|
||||
second.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SES-004: a client that only streams (no unary call) for longer than the lease must still
|
||||
/// get its full detach-grace window when the stream drops. The detach renews the lease, so
|
||||
/// the session is neither lease-expired nor grace-expired until the grace elapses, and is
|
||||
/// then closed for detach-grace rather than for the lease.
|
||||
/// </summary>
|
||||
/// <returns>A task that represents the asynchronous operation.</returns>
|
||||
[Fact]
|
||||
public async Task DetachGrace_StreamOutlivesLease_DetachStillGetsFullGraceWindow()
|
||||
{
|
||||
FakeTimeProvider clock = new(DateTimeOffset.UtcNow);
|
||||
FakeWorkerClient workerClient = new();
|
||||
await using GatewaySession session = CreateReadySessionWithDetachGrace(
|
||||
workerClient,
|
||||
clock,
|
||||
detachGrace: TimeSpan.FromSeconds(30));
|
||||
|
||||
IEventSubscriberLease lease = session.AttachEventSubscriber(maxSubscribers: 1);
|
||||
|
||||
// Stream for longer than the 30-minute lease with no unary command.
|
||||
clock.Advance(TimeSpan.FromMinutes(31));
|
||||
lease.Dispose();
|
||||
|
||||
clock.Advance(TimeSpan.FromSeconds(5));
|
||||
Assert.False(session.IsLeaseExpired(clock.GetUtcNow()));
|
||||
Assert.False(session.IsDetachGraceExpired(clock.GetUtcNow()));
|
||||
Assert.False(session.TryBeginCloseIfExpired(clock.GetUtcNow(), out _));
|
||||
Assert.Equal(SessionState.Ready, session.State);
|
||||
|
||||
clock.Advance(TimeSpan.FromSeconds(25));
|
||||
Assert.False(session.IsLeaseExpired(clock.GetUtcNow()));
|
||||
Assert.True(session.IsDetachGraceExpired(clock.GetUtcNow()));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// SES-007: a successful <c>Unregister</c> drops every item registration under that server
|
||||
/// handle (MXAccess invalidates them all), and leaves other server handles alone.
|
||||
/// </summary>
|
||||
[Fact]
|
||||
public void TrackCommandReply_SuccessfulUnregister_PurgesThatServerHandlesItems()
|
||||
{
|
||||
GatewaySession session = CreateSession();
|
||||
TrackAddItem(session, serverHandle: 1, itemHandle: 10, "A.PV");
|
||||
TrackAddItem(session, serverHandle: 1, itemHandle: 11, "B.PV");
|
||||
TrackAddItem(session, serverHandle: 1, itemHandle: 12, "C.PV");
|
||||
TrackAddItem(session, serverHandle: 2, itemHandle: 10, "D.PV");
|
||||
|
||||
session.TrackCommandReply(
|
||||
new MxCommand { Kind = MxCommandKind.Unregister, Unregister = new UnregisterCommand { ServerHandle = 1 } },
|
||||
new MxCommandReply { ProtocolStatus = MxAccessGrpcMapper.Ok() });
|
||||
|
||||
Assert.False(session.TryGetItemRegistration(1, 10, out _));
|
||||
Assert.False(session.TryGetItemRegistration(1, 11, out _));
|
||||
Assert.False(session.TryGetItemRegistration(1, 12, out _));
|
||||
Assert.True(session.TryGetItemRegistration(2, 10, out SessionItemRegistration other));
|
||||
Assert.Equal("D.PV", other.TagAddress);
|
||||
}
|
||||
|
||||
/// <summary>A failed <c>Unregister</c> leaves the registrations in place.</summary>
|
||||
[Fact]
|
||||
public void TrackCommandReply_FailedUnregister_KeepsRegistrations()
|
||||
{
|
||||
GatewaySession session = CreateSession();
|
||||
TrackAddItem(session, serverHandle: 1, itemHandle: 10, "A.PV");
|
||||
|
||||
session.TrackCommandReply(
|
||||
new MxCommand { Kind = MxCommandKind.Unregister, Unregister = new UnregisterCommand { ServerHandle = 1 } },
|
||||
new MxCommandReply { ProtocolStatus = new ProtocolStatus { Code = ProtocolStatusCode.MxaccessFailure } });
|
||||
|
||||
Assert.True(session.TryGetItemRegistration(1, 10, out _));
|
||||
}
|
||||
|
||||
private static void TrackAddItem(GatewaySession session, int serverHandle, int itemHandle, string tagAddress)
|
||||
{
|
||||
session.TrackCommandReply(
|
||||
new MxCommand
|
||||
{
|
||||
Kind = MxCommandKind.AddItem,
|
||||
AddItem = new AddItemCommand { ServerHandle = serverHandle, ItemDefinition = tagAddress },
|
||||
},
|
||||
new MxCommandReply
|
||||
{
|
||||
ProtocolStatus = MxAccessGrpcMapper.Ok(),
|
||||
AddItem = new AddItemReply { ItemHandle = itemHandle },
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// With detach-grace disabled (<c>0</c>), dropping the last subscriber must
|
||||
/// match today's behavior: the session stays <see cref="SessionState.Ready"/> with no
|
||||
|
||||
Reference in New Issue
Block a user