Skip to content

Buffering Throttle #330

Open
Open
@thecoolwinter

Description

@thecoolwinter

I know there's ongoing discussion about throttle semantics, but this is an API I can see being useful even while the final throttle API is being nailed down. Also correct me if this is already possible with the existing API.

One use case for throttle is to limit the reception of new items in the stream, while still receiving all items produced by the stream. For instance, receiving a stream of bytes and only processing the accumulated bytes for every time interval.

This is different from both the existing stream.buffer and stream._throttle apis because the elements of the resulting stream would be an array of the original elements. Existing buffering streams produce a single element in each iteration, and throttle only produces the last(ish) element received. This would produce all elements received in the time period since the last produced value.

A potential API might look like:

extension AsyncSequence {
    public func throttleBuffering<C: Clock>(for interval: C.Instant.Duration, clock: C, bufferPolicy: BufferPolicy) -> _BufferingThrottledStream<Element>
}

Where the _BufferingThrottledStream produces arrays of Element at each time interval if there are any elements to produce. It would also need to have a buffering policy similar to the existing buffering stream iterators.

This could maybe use some of the already implemented buffering streams and every time interval grab all buffered elements and send them downstream.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions