feat: add bounded shared grpc subscriptions and delivery scheduling

This commit is contained in:
Joseph Doherty
2026-11-16 07:31:57 -05:00
parent 09f4f18d0e
commit af30a0266a
51 changed files with 9182 additions and 184 deletions
@@ -21,7 +21,7 @@ namespace ZB.MOM.WW.OtOpcUa.Driver.Modbus;
/// <see cref="PollGroupEngine"/> since Modbus has no native push model.
/// </remarks>
public sealed class ModbusDriver
: IDriver, ITagDiscovery, IReadable, IWritable, IWriteOutcomeCapability, IBatchSubscribable, IHostConnectivityProbe, IPerCallHostResolver, ISkippedTagReporter, IDisposable, IAsyncDisposable
: IDriver, ITagDiscovery, IReadable, IWritable, IWriteOutcomeCapability, IBatchSubscribable, IAcknowledgedAcquisitionSource, IHostConnectivityProbe, IPerCallHostResolver, ISkippedTagReporter, IDisposable, IAsyncDisposable
{
/// <summary>Authored tags this driver could not build into its poll table.</summary>
private readonly SkippedTagLedger _skippedTags = new();
@@ -232,6 +232,10 @@ public sealed class ModbusDriver
/// <summary>Upper bound on the poll-loop failure backoff — adopts the S7-proven 30 s cap fleet-wide.</summary>
private static readonly TimeSpan PollBackoffCap = TimeSpan.FromSeconds(30);
// Task.Delay's largest supported timer duration. Acquisition acknowledgments must never
// promise a cadence the polling loop cannot schedule.
private static readonly TimeSpan MaxAcquisitionInterval = TimeSpan.FromMilliseconds(uint.MaxValue - 1d);
/// <summary>
/// Routes a poll-loop reader failure to the driver health surface: logs it and
/// degrades to <see cref="DriverState.Degraded"/> preserving <c>LastSuccessfulRead</c>.
@@ -2017,13 +2021,47 @@ public sealed class ModbusDriver
Task.FromResult(_poll.Subscribe(fullReferences, publishingInterval));
/// <inheritdoc />
public async Task UnsubscribeAsync(ISubscriptionHandle handle, CancellationToken cancellationToken)
/// <remarks>The polling engine uses a bounded drain during teardown. Consumers must fence late
/// callbacks if a reader ignores cancellation or a callback blocks beyond that bound. Intervals
/// above the polling timer's maximum supported duration are rejected.</remarks>
public async Task<AcquisitionSubscriptionResult> SubscribeAcquisitionAsync(
AcquisitionSubscriptionRequest request, Action<AcquisitionBatch> onAcquired, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(request);
ArgumentNullException.ThrowIfNull(onAcquired);
ArgumentNullException.ThrowIfNull(request.FullReferences);
if (request.FullReferences.Count == 0 || request.FullReferences.Any(string.IsNullOrWhiteSpace))
throw new ArgumentException("At least one nonempty reference is required.", nameof(request));
if (request.RequestedSamplingInterval < TimeSpan.Zero || request.RequestedSamplingInterval > MaxAcquisitionInterval)
throw new ArgumentOutOfRangeException(nameof(request), "Sampling interval is outside the supported timer range.");
if (request.DesiredRevision <= 0 || request.ProducerIncarnation <= 0)
throw new ArgumentOutOfRangeException(nameof(request), "Revision and producer incarnation must be positive.");
cancellationToken.ThrowIfCancellationRequested();
var handle = _poll.Subscribe(request.FullReferences, request.RequestedSamplingInterval,
items => onAcquired(new AcquisitionBatch(request.DesiredRevision, request.ProducerIncarnation, items)));
if (cancellationToken.IsCancellationRequested)
{
await UnsubscribeCoreAsync(handle).ConfigureAwait(false);
cancellationToken.ThrowIfCancellationRequested();
}
var nominal = request.RequestedSamplingInterval < PollGroupEngine.DefaultMinInterval
? PollGroupEngine.DefaultMinInterval : request.RequestedSamplingInterval;
return new AcquisitionSubscriptionResult(handle, nominal, request.DesiredRevision, request.ProducerIncarnation);
}
/// <inheritdoc />
public Task UnsubscribeAsync(ISubscriptionHandle handle, CancellationToken cancellationToken) =>
UnsubscribeCoreAsync(handle);
private async Task UnsubscribeCoreAsync(ISubscriptionHandle handle)
{
// Async teardown: the engine drains the in-flight poll without blocking the caller's
// thread — this runs inside a DriverInstanceActor mailbox turn.
await _poll.UnsubscribeAsync(handle).ConfigureAwait(false);
// Drop any partially-collected cycle buffer so a torn-down subscription leaks nothing.
// The teardown above awaits the loop task, so no onChange can re-add after this point.
// Drop any partially-collected cycle buffer. The bounded drain normally finishes the loop;
// a callback blocked beyond its timeout may still finish late and must be fenced by consumers.
_cycleBatchItems.TryRemove(handle, out _);
}