feat(grpc-data): A2a — byte-bounded condition chunks (256 KiB estimate); DataConnections.md condition streams

This commit is contained in:
Joseph Doherty
2026-11-24 12:30:28 -05:00
parent 6572ee83bc
commit 1977d8ce84
2 changed files with 61 additions and 5 deletions
@@ -39,6 +39,9 @@ internal sealed class ConditionItemStream
/// <summary>The most records one chunk carries.</summary>
internal const int MaxRecordsPerChunk = 128;
/// <summary>The most estimated bytes one chunk carries (the transport reserves ≤ the session's pending bytes per frame).</summary>
internal const long MaxChunkBytes = 256 * 1024;
/// <summary>The least time between two snapshots of one item (re-snapshot storms after an overflow).</summary>
internal static readonly TimeSpan MinResnapshotInterval = TimeSpan.FromSeconds(1);
@@ -196,8 +199,7 @@ internal sealed class ConditionItemStream
{ _state = State.Live; return new ConditionChunk(true, _total, [], _snapshotComplete, Snapshots, EndsSnapshot: true); }
return new ConditionChunk(true, _total, [], false);
}
var batch = new List<DataConditionState>(Math.Min(MaxRecordsPerChunk, _snapshotRecords.Count));
while (batch.Count < MaxRecordsPerChunk && _snapshotRecords.TryDequeue(out var record)) batch.Add(record);
var batch = Take(_snapshotRecords);
var last = _snapshotRecords.Count == 0;
if (last) _state = State.Live;
return new ConditionChunk(true, null, batch, last && _snapshotComplete, Snapshots, EndsSnapshot: last);
@@ -240,11 +242,31 @@ internal sealed class ConditionItemStream
private ConditionChunk? NextDeltaChunk()
{
if (_pendingDeltas.Count == 0) return null;
var batch = new List<DataConditionState>(Math.Min(MaxRecordsPerChunk, _pendingDeltas.Count));
while (batch.Count < MaxRecordsPerChunk && _pendingDeltas.TryDequeue(out var record)) batch.Add(record);
return new ConditionChunk(false, null, batch, false);
return new ConditionChunk(false, null, Take(_pendingDeltas), false);
}
/// <summary>At most <see cref="MaxRecordsPerChunk"/> records and <see cref="MaxChunkBytes"/> estimated bytes (at least one).</summary>
private static List<DataConditionState> Take(Queue<DataConditionState> queue)
{
var batch = new List<DataConditionState>(Math.Min(MaxRecordsPerChunk, queue.Count));
long bytes = 0;
while (batch.Count < MaxRecordsPerChunk && queue.TryPeek(out var next))
{
var size = EstimatedBytes(next);
if (batch.Count > 0 && bytes + size > MaxChunkBytes) break;
batch.Add(queue.Dequeue());
bytes += size;
}
return batch;
}
/// <summary>The transport's per-record reservation (the host's <c>GrpcDeliverySink</c> charges the same shape).</summary>
internal static long EstimatedBytes(DataConditionState r) => 2048L + 4L * (r.ConditionKey.Length + r.Message.Length
+ (r.Comment?.Length ?? 0) + (r.ClientUserId?.Length ?? 0) + (r.Activation?.Incarnation.Length ?? 0)
+ (r.Info is { } info ? info.ConditionType.Length + info.ConditionName.Length
+ info.Placements.Sum(p => 256 + p.Subject.Length + p.Address.Identifier.Length + p.Address.NamespaceUri.Length
+ p.Address.OriginClusterId.Length) : 0));
/// <summary>A new catalog: conditions leaving the listing get a removal record; joining ones a small snapshot as deltas.</summary>
private void ApplyScopeChange(ConditionItemScope scope)
{