Class ThrottleTransformer<T>

Namespace
Wolfgang.Etl.Transformers
Assembly
Wolfgang.Etl.Transformers.dll

Paces an asynchronous sequence so that successive items are yielded no closer together than a minimum interval, without changing the stream's shape or order. Useful for rate-limiting a downstream sink (an API, a database) that must not be hit too fast.

public sealed class ThrottleTransformer<T> : ITransformAsync<T, T> where T : notnull

Type Parameters

T

The type of items flowing through the transformer. Must be non-null.

Inheritance
ThrottleTransformer<T>
Implements
ITransformAsync<T, T>
Inherited Members
Extension Methods

Examples

var throttle = new ThrottleTransformer<Request>(TimeSpan.FromMilliseconds(200));
await foreach (var request in throttle.TransformAsync(source))
{
    // at most ~5 items/second
}

Remarks

The pacing is adaptive: the delay before an item is the remaining time until the minimum interval since the previous item has elapsed, so a consumer that was already slow is not delayed further. The first item is never delayed. All waits observe the enumeration's CancellationToken (supply it via .WithCancellation(token)).

Constructors

ThrottleTransformer(TimeSpan)

Initializes a new instance of the ThrottleTransformer<T> class.

public ThrottleTransformer(TimeSpan minInterval)

Parameters

minInterval TimeSpan

The minimum time between successive yielded items. Zero disables pacing (a pass-through).

Exceptions

ArgumentOutOfRangeException

minInterval is negative.

Methods

TransformAsync(IAsyncEnumerable<T>)

Asynchronously yields each item from items in order, pacing successive items to at least the configured minimum interval apart.

public IAsyncEnumerable<T> TransformAsync(IAsyncEnumerable<T> items)

Parameters

items IAsyncEnumerable<T>

The asynchronous source sequence.

Returns

IAsyncEnumerable<T>

An asynchronous sequence with the same items, in the same order, paced apart.

Exceptions

ArgumentNullException

items is null.