apple / apple/swift-async-algorithms
Buffering Throttle
- Dominant language
- Swift
- Stars
- 3.7k
- Forks
- 226
- Avg merge
- 10d 3h
- Merged PRs (30d)
- 1
Description
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:
```swift
extension AsyncSequence {
public func throttleBuffering(for interval: C.Instant.Duration, clock: C, bufferPolicy: BufferPolicy) -> _BufferingThrottledStream
}
```
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.
Contributor guide
Research direction
Start by reading the existing stream.buffer and stream._throttle APIs, then inspect the buffering stream iterators and their buffering policies. Compare their element and timing behavior with the proposed throttleBuffering API; done would mean an agreed implementation that emits arrays of all elements received during each interval while respecting the chosen buffering policy.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- swift
- Domain
- api
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100