fix(gateway): bound the session replay ring by bytes as well as count (mxaccessgw#172)
ci / nightly-windev (push) Has been skipped
ci / windows-x86 (push) Successful in 1m26s
ci / java (push) Successful in 2m28s
ci / portable (push) Failing after 4m48s
ci / nightly-windev (push) Has been skipped
ci / windows-x86 (push) Successful in 1m26s
ci / java (push) Successful in 2m28s
ci / portable (push) Failing after 4m48s
The replay ring was bounded only by ReplayBufferCapacity (1024 events) and ReplayRetentionSeconds. Event size is unbounded up to the worker frame size, so a burst of large-array events could pin gigabytes per session while the count looked small. New MxGateway:Events:ReplayBufferMaxBytes (default 64 MiB, 0 disables) caps the summed serialized size (MxEvent.CalculateSize, measured only when the cap is on). Over the cap the oldest entries are evicted, exactly like the count cap, so a resuming subscriber sees the existing ReplayGap path; an event bigger than the whole cap is not retained. Every eviction (count, age or byte) now clears its slot, so an evicted event is released immediately instead of staying referenced until the slot is reused. The 64 MiB default is a judgement call: it never binds for scalar traffic (1024 x a few hundred bytes) and only trims large-array bursts. Tests: SessionEventDistributorTests byte-cap eviction, single oversized event, cap 0 = count only (the first two fail with the cap enforcement removed); validator rejects a negative value. Docs: GatewayConfiguration.md, Sessions.md, DesignDecisions.md; appsettings.json carries the key. Claude-Session: https://claude.ai/code/session_01GT67AEHd2Acv1YPwGfXEoV
This commit is contained in:
@@ -137,8 +137,14 @@ public sealed class SessionEventDistributor : IAsyncDisposable
|
||||
// event) and _replayCount entries follow it, wrapping modulo the array length. The
|
||||
// logical entry at position i is _replayBuffer[(_replayHead + i) % Length]. Capacity
|
||||
// == 0 disables retention (the array is empty and never indexed); RetentionSeconds
|
||||
// <= 0 disables age-based eviction.
|
||||
// <= 0 disables age-based eviction. _replayMaxBytes > 0 additionally bounds the summed
|
||||
// serialized size of the retained events (_replayBytes), evicting oldest-first
|
||||
// (mxaccessgw#172); 0 disables the byte cap. Every eviction clears its slot so an evicted
|
||||
// event — possibly a multi-MB array — is released at once rather than when the slot is
|
||||
// next overwritten.
|
||||
private readonly int _replayBufferCapacity;
|
||||
private readonly long _replayMaxBytes;
|
||||
private long _replayBytes;
|
||||
private readonly TimeSpan _replayRetention;
|
||||
private readonly bool _ageEvictionEnabled;
|
||||
private readonly ReplayEntry[] _replayBuffer;
|
||||
@@ -246,6 +252,11 @@ public sealed class SessionEventDistributor : IAsyncDisposable
|
||||
/// handler. When <see langword="null"/> (unit/skeleton use) the offending subscriber is
|
||||
/// still disconnected but no metric/fault side effect runs.
|
||||
/// </param>
|
||||
/// <param name="replayBufferMaxBytes">
|
||||
/// Maximum summed serialized size, in bytes, of the retained replay events. When an append
|
||||
/// takes the total above it the oldest entries are evicted first — including the new one
|
||||
/// itself if it alone exceeds the cap. <c>0</c> (the default) disables the byte cap.
|
||||
/// </param>
|
||||
/// <param name="singleSubscriberMode">
|
||||
/// <see langword="true"/> when the owning session is in single-subscriber mode
|
||||
/// (<c>AllowMultipleEventSubscribers == false</c>). This gates the FailFast
|
||||
@@ -266,13 +277,15 @@ public sealed class SessionEventDistributor : IAsyncDisposable
|
||||
ILogger<SessionEventDistributor> logger,
|
||||
TimeProvider timeProvider,
|
||||
SubscriberOverflowHandler? overflowHandler = null,
|
||||
bool singleSubscriberMode = true)
|
||||
bool singleSubscriberMode = true,
|
||||
long replayBufferMaxBytes = 0)
|
||||
{
|
||||
ArgumentException.ThrowIfNullOrWhiteSpace(sessionId);
|
||||
ArgumentNullException.ThrowIfNull(eventSourceFactory);
|
||||
ArgumentOutOfRangeException.ThrowIfLessThan(subscriberQueueCapacity, 1);
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(replayBufferCapacity);
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(replayRetentionSeconds);
|
||||
ArgumentOutOfRangeException.ThrowIfNegative(replayBufferMaxBytes);
|
||||
ArgumentNullException.ThrowIfNull(logger);
|
||||
ArgumentNullException.ThrowIfNull(timeProvider);
|
||||
|
||||
@@ -284,6 +297,7 @@ public sealed class SessionEventDistributor : IAsyncDisposable
|
||||
_shutdownTimeout = DefaultShutdownTimeout;
|
||||
_replayBufferCapacity = replayBufferCapacity;
|
||||
_replayBuffer = new ReplayEntry[replayBufferCapacity];
|
||||
_replayMaxBytes = replayBufferMaxBytes;
|
||||
_ageEvictionEnabled = replayRetentionSeconds > 0;
|
||||
_replayRetention = _ageEvictionEnabled
|
||||
? TimeSpan.FromSeconds(replayRetentionSeconds)
|
||||
@@ -946,7 +960,10 @@ public sealed class SessionEventDistributor : IAsyncDisposable
|
||||
// Append at the logical tail. When the ring is full the oldest entry is
|
||||
// overwritten in place (its slot becomes the new tail) and the head advances,
|
||||
// so the newest _replayBufferCapacity events are retained with no allocation.
|
||||
ReplayEntry entry = new(mxEvent, _timeProvider.GetUtcNow());
|
||||
// The size is only measured when the byte cap is on, so the cap-off path
|
||||
// adds no serialization work to the pump.
|
||||
int sizeBytes = _replayMaxBytes > 0 ? mxEvent.CalculateSize() : 0;
|
||||
ReplayEntry entry = new(mxEvent, _timeProvider.GetUtcNow(), sizeBytes);
|
||||
if (_replayCount < _replayBufferCapacity)
|
||||
{
|
||||
_replayBuffer[(_replayHead + _replayCount) % _replayBufferCapacity] = entry;
|
||||
@@ -954,11 +971,14 @@ public sealed class SessionEventDistributor : IAsyncDisposable
|
||||
}
|
||||
else
|
||||
{
|
||||
_replayBytes -= _replayBuffer[_replayHead].SizeBytes;
|
||||
_replayBuffer[_replayHead] = entry;
|
||||
_replayHead = (_replayHead + 1) % _replayBufferCapacity;
|
||||
}
|
||||
|
||||
_replayBytes += sizeBytes;
|
||||
EvictAged();
|
||||
EvictOverByteCap();
|
||||
}
|
||||
|
||||
// Single capture point for both the retained and no-retention paths.
|
||||
@@ -982,12 +1002,38 @@ public sealed class SessionEventDistributor : IAsyncDisposable
|
||||
DateTimeOffset cutoff = _timeProvider.GetUtcNow() - _replayRetention;
|
||||
while (_replayCount > 0 && _replayBuffer[_replayHead].RetainedAt < cutoff)
|
||||
{
|
||||
_replayHead = (_replayHead + 1) % _replayBuffer.Length;
|
||||
_replayCount--;
|
||||
EvictOldest();
|
||||
}
|
||||
}
|
||||
|
||||
private readonly record struct ReplayEntry(MxEvent Event, DateTimeOffset RetainedAt);
|
||||
// Must be called under _replayLock. Drops the oldest entries until the retained events'
|
||||
// summed serialized size is within _replayMaxBytes (mxaccessgw#172). A single event larger
|
||||
// than the cap is itself evicted, leaving the ring empty; the resume path then reports a gap
|
||||
// anchored on the highest sequence seen, as for any other eviction.
|
||||
private void EvictOverByteCap()
|
||||
{
|
||||
if (_replayMaxBytes <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
while (_replayCount > 0 && _replayBytes > _replayMaxBytes)
|
||||
{
|
||||
EvictOldest();
|
||||
}
|
||||
}
|
||||
|
||||
// Must be called under _replayLock with _replayCount > 0. Clears the slot so the evicted
|
||||
// event is unreferenced immediately.
|
||||
private void EvictOldest()
|
||||
{
|
||||
_replayBytes -= _replayBuffer[_replayHead].SizeBytes;
|
||||
_replayBuffer[_replayHead] = default;
|
||||
_replayHead = (_replayHead + 1) % _replayBuffer.Length;
|
||||
_replayCount--;
|
||||
}
|
||||
|
||||
private readonly record struct ReplayEntry(MxEvent Event, DateTimeOffset RetainedAt, int SizeBytes);
|
||||
|
||||
private sealed class Subscriber(long id, Channel<MxEvent> channel, bool isInternal)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user