Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffering Throttle #330

Open
thecoolwinter opened this issue Sep 24, 2024 · 1 comment
Open

Buffering Throttle #330

thecoolwinter opened this issue Sep 24, 2024 · 1 comment

Comments

@thecoolwinter
Copy link

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.

@thecoolwinter thecoolwinter changed the title Accumulating Throttle Buffering Throttle Sep 24, 2024
@thecoolwinter
Copy link
Author

I suppose a workaround right now is to use a chunked stream with a timer. However this still sends events when there's no items being sent from the producer:

for await events in stream.chunked(by: .repeating(every: .milliseconds(250), clock: .continuous)) {
    // Accumulated data
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant