Class OutputDispatcher

Dispatches published values to a set of registered subscribers.

Inheritance
OutputDispatcher
Namespace: MOSAIC.Components.Basics
Assembly: MOSAIC.dll
Syntax
public sealed class OutputDispatcher : IOutputDispatcher, IDisposable, IAsyncDisposable
Remarks

Fan-out mechanism: when a publisher calls Dispatch(object, object), the value is queued for every subscriber and delivered by that subscriber's own dedicated consumer pump.

Per-subscriber ordering and serialization: each subscriber owns a bounded Channel<T> drained by a single long-running pump, so a subscriber observes values in publish order from this dispatcher, with one ReceiveInput(object, object) call at a time on that subscription. A subscriber connected to several publishers has several independent pumps and must protect shared state against concurrent calls. Different subscribers also run on independent pumps, so branch parallelism is preserved.

Non-blocking delivery: Dispatch(object, object) only enqueues (a lock-free TryWrite per subscriber) and never blocks the publishing thread, even when a subscriber is slow. Unlike a per-value Task.Run fan-out, there is no per-value task or closure allocation.

Backpressure: Each processing subscription has a bounded queue. On overflow, that branch stops accepting input and reports a fault. Already accepted values drain in order. Reconnect the branch after addressing the overload; processing never silently skips a gap.

Error handling: an exception thrown by a subscriber is caught and logged through the shared logger; it does not stop that subscriber's pump or affect other subscribers.

Thread-safety: the subscriber set is an immutable copy-on-write array guarded by _gate. AddSubscriber(ISubscriber)/RemoveSubscriber(ISubscriber) publish a new array under the lock; Dispatch(object, object) reads it via a single volatile read with no locking, so adding or removing a subscriber concurrently with a dispatch is safe.

Lifetime: Dispose() completes every channel; each pump drains its backlog and exits. Owners must dispose the dispatcher (e.g. from the publishing block's Dispose) or the pump tasks will stay alive waiting for input.

Constructors

OutputDispatcher(int)

Bounds each processing branch; overflow faults the branch until reconnected.

Declaration
public OutputDispatcher(int queueCapacity = 8192)
Parameters
Type Name Description
int queueCapacity

Fields

_capacity

Declaration
private readonly int _capacity
Field Value
Type Description
int

_completion

Declaration
private Task _completion
Field Value
Type Description
Task

_disposed

Declaration
private volatile bool _disposed
Field Value
Type Description
bool

_failures

Declaration
private long _failures
Field Value
Type Description
long

_gate

Declaration
private readonly object _gate
Field Value
Type Description
object

_pumps

Declaration
private readonly List<Task> _pumps
Field Value
Type Description
List<Task>

_rejected

Declaration
private long _rejected
Field Value
Type Description
long

_subs

Declaration
private volatile OutputDispatcher.Subscription[] _subs
Field Value
Type Description
Subscription[]

Properties

QueuedValues

Declaration
public int QueuedValues { get; }
Property Value
Type Description
int

RejectedValues

Declaration
public long RejectedValues { get; }
Property Value
Type Description
long

SubscriberFailures

Declaration
public long SubscriberFailures { get; }
Property Value
Type Description
long

Methods

AddSubscriber(ISubscriber)

Registers a subscriber so it will receive values dispatched by this instance.

Declaration
public void AddSubscriber(ISubscriber s)
Parameters
Type Name Description
ISubscriber s

The subscriber to add.

Remarks

If the subscriber is already registered, or the dispatcher has been disposed, this has no effect.

Exceptions
Type Condition
ArgumentNullException

Thrown if s is null.

Dispatch(object, object)

Forwards a published value to all registered subscribers by enqueuing it on each subscriber's mailbox. Never blocks the publishing thread; delivery happens on the per-subscriber pumps.

Declaration
public void Dispatch(object sender, object value)
Parameters
Type Name Description
object sender

The publisher that originated the value.

object value

The value payload to forward.

Dispose()

Completes every subscriber mailbox so the pumps drain their backlog and exit. Does not block the caller waiting for the pumps to finish.

Declaration
public void Dispose()

DisposeAsync()

Completes delivery and waits for all active and removed subscriber pumps.

Declaration
public ValueTask DisposeAsync()
Returns
Type Description
ValueTask

IndexOf(Subscription[], ISubscriber)

Declaration
private static int IndexOf(OutputDispatcher.Subscription[] subs, ISubscriber s)
Parameters
Type Name Description
Subscription[] subs
ISubscriber s
Returns
Type Description
int

RemoveSubscriber(ISubscriber)

Unregisters a subscriber so it no longer receives dispatched values. Any values already queued for it are still delivered (in order) before its pump exits.

Declaration
public void RemoveSubscriber(ISubscriber s)
Parameters
Type Name Description
ISubscriber s

The subscriber to remove.

Exceptions
Type Condition
ArgumentNullException

Thrown if s is null.

RunPump(Subscription)

Declaration
private Task RunPump(OutputDispatcher.Subscription sub)
Parameters
Type Name Description
OutputDispatcher.Subscription sub
Returns
Type Description
Task

Implements

IOutputDispatcher
IDisposable
IAsyncDisposable

Search documentation

Start typing to search the documentation.