Class AsyncCoalescer
Coalesces concurrent callers into a single invocation of the asynchronous action.
Implements
Inherited Members
Namespace: DotNext.Threading
Assembly: DotNext.Threading.dll
Syntax
public abstract class AsyncCoalescer : IAsyncDisposable
Remarks
The first caller of WaitAsync(CancellationToken) opens the time window. The callers arriving within the window are suspended and join the same batch. When the window expires, or Flush() is called, the window is closed, and the action is invoked once for all the callers of the batch. The suspended callers are resumed when the action completes.
The action always starts after all the callers of the batch have joined it. Therefore, the completion of the action observed by the caller is caused by the invocation started after the call to WaitAsync(CancellationToken). The callers arriving while the action is in progress open the next window and never observe the completion of the action started before them.
The window is fixed: it's not extended by the callers arriving within it. The next window is opened by the first caller after the closure of the previous window, so the actions of the consecutive windows can run concurrently.
The action runs on the thread pool without the ExecutionContext of the callers.
Constructors
View SourceAsyncCoalescer(TimeSpan, TimeProvider)
Initializes a new coalescer.
Declaration
protected AsyncCoalescer(TimeSpan delay, TimeProvider provider)
Parameters
| Type | Name | Description |
|---|---|---|
| TimeSpan | delay | The duration of the time window, measured from the arrival of its first caller. InfiniteTimeSpan means that the window is closed only by Flush(). |
| TimeProvider | provider | The time provider used to create the timer of the window. |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException |
|
Properties
View SourceConcurrencyLevel
Gets or sets the maximum expected number of suspender callers in a batch.
Declaration
public long ConcurrencyLevel { get; init; }
Property Value
| Type | Description |
|---|---|
| long |
Exceptions
| Type | Condition |
|---|---|
| ArgumentOutOfRangeException |
|
Methods
View SourceCompleteAsync(bool)
Performs the action for all the callers of the closed window.
Declaration
protected abstract Task CompleteAsync(bool triggeredManually)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | triggeredManually | true if the window is closed by Flush(); false if the window is expired. |
Returns
| Type | Description |
|---|---|
| Task | The task representing asynchronous execution of the action. |
Remarks
The method is called on the thread pool. Multiple invocations can run concurrently, because the next window can be closed while the action of the previous window is still in progress. The exception thrown by the method is passed to all the callers of the window.
DisposeAsync()
Disposes the coalescer.
Declaration
public ValueTask DisposeAsync()
Returns
| Type | Description |
|---|---|
| ValueTask | The task representing asynchronous execution of this method. |
Remarks
The callers of the open window are resumed with ObjectDisposedException. The actions in progress are not interrupted, and this method doesn't wait for them: they resume the callers of their windows with their own results.
Flush()
Closes the open window immediately, without waiting for its expiration, and starts the action.
Declaration
public void Flush()
Remarks
The method doesn't wait for the action to complete. It does nothing if there is no open window, or the coalescer has been disposed.
WaitAsync(CancellationToken)
Suspends the caller until the action is completed for the window that the caller joins.
Declaration
public ValueTask<bool> WaitAsync(CancellationToken token = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | token | The token that can be used to cancel the waiting. |
Returns
| Type | Description |
|---|---|
| ValueTask<bool> | true if the window has been closed by Flush(); false if the window has been expired. |
Remarks
The caller opens a new window if there is no open window. The action invoked for the window starts after this method is called.
Exceptions
| Type | Condition |
|---|---|
| OperationCanceledException | The operation has been canceled. |
| ObjectDisposedException | The coalescer has been disposed. |
| Exception | The exception thrown by CompleteAsync(bool). |