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

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:
Joseph Doherty
2026-11-09 12:38:22 -05:00
parent 7b21f304e9
commit 07badd139e
12 changed files with 186 additions and 17 deletions
@@ -4,4 +4,5 @@ public sealed record EffectiveEventConfiguration(
int QueueCapacity,
string BackpressurePolicy,
int ReplayBufferCapacity,
double ReplayRetentionSeconds);
double ReplayRetentionSeconds,
long ReplayBufferMaxBytes);
@@ -28,6 +28,21 @@ public sealed class EventOptions
/// </summary>
public double ReplayRetentionSeconds { get; init; } = 300;
/// <summary>
/// Gets the maximum total serialized size, in bytes, of the events retained in the
/// per-session replay ring buffer. The count cap alone does not bound memory: a burst of
/// large-array events can make <see cref="ReplayBufferCapacity"/> events many times the
/// size typical traffic suggests (up to the worker frame size each). When the retained
/// total exceeds this cap the oldest events are evicted first, exactly as for the count
/// cap, so a resuming subscriber sees the usual <c>ReplayGap</c> sentinel. The default of
/// 64 MiB never binds for ordinary scalar traffic (1,024 events of a few hundred bytes)
/// and only trims bursts of large events. A value of <c>0</c> disables the byte cap.
/// </summary>
public long ReplayBufferMaxBytes { get; init; } = DefaultReplayBufferMaxBytes;
/// <summary>Default for <see cref="ReplayBufferMaxBytes"/> (64 MiB).</summary>
public const long DefaultReplayBufferMaxBytes = 64L * 1024 * 1024;
/// <summary>
/// Gets the maximum <c>total_length</c> a sparse-array write may declare before the
/// gateway rejects it with <c>InvalidArgument</c>, enforced in
@@ -56,7 +56,8 @@ public sealed class GatewayConfigurationProvider(IOptions<GatewayOptions> option
QueueCapacity: value.Events.QueueCapacity,
BackpressurePolicy: value.Events.BackpressurePolicy.ToString(),
ReplayBufferCapacity: value.Events.ReplayBufferCapacity,
ReplayRetentionSeconds: value.Events.ReplayRetentionSeconds),
ReplayRetentionSeconds: value.Events.ReplayRetentionSeconds,
ReplayBufferMaxBytes: value.Events.ReplayBufferMaxBytes),
Dashboard: new EffectiveDashboardConfiguration(
Enabled: value.Dashboard.Enabled,
AllowAnonymousLocalhost: value.Dashboard.AllowAnonymousLocalhost,
@@ -385,6 +385,9 @@ public sealed class GatewayOptionsValidator : OptionsValidatorBase<GatewayOption
builder.RequireThat(
options.ReplayRetentionSeconds >= 0,
"MxGateway:Events:ReplayRetentionSeconds must be greater than or equal to zero.");
builder.RequireThat(
options.ReplayBufferMaxBytes >= 0,
"MxGateway:Events:ReplayBufferMaxBytes must be greater than or equal to zero.");
builder.RequireThat(
options.MaxSparseArrayLength >= 1 && options.MaxSparseArrayLength <= Array.MaxLength,