* Rebase guides WIP to avoid source changes * Add prose for debounce and throttle * Add an alterantive for debounce * Starts to prose for reductions * Add effect listings for all types * Add some examples for reductions * Update Guides/Channel.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Update Guides/Channel.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Update Guides/Channel.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Update Guides/Channel.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Update Guides/Collections.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Update Guides/Throttle.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Update Guides/Throttle.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Update Guides/Throttle.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Update Sources/AsyncAlgorithms/AsyncInclusiveReductionsSequence.swift Co-authored-by: Chris Adamson <invalidname@gmail.com> * Adjust events to elements for channels * Add a bit more detail to the README and a start to the guide for Task.select * Chunked updates * Add a getting started section to the README * Add some more prose for Channel * Update Guides/RemoveDuplicates.md Co-authored-by: Chris Adamson <invalidname@gmail.com> * Apply suggestions from code review Co-authored-by: Chris Adamson <invalidname@gmail.com> * Add some brief prose for Compacted * reductions could be named scan * spelling and correctness fixes * Apply @kperryua's suggestion for changing the intro of Compacted * Add some brief prose for Timer * Apply suggestions from code review for README Co-authored-by: Tony Parker <anthony.parker@apple.com> * Add some brief prose for RemoveDuplicates * Fleshing out of `Joined` Co-authored-by: Chris Adamson <invalidname@gmail.com> Co-authored-by: Kevin Perry <kperry@apple.com> Co-authored-by: Tony Parker <anthony.parker@apple.com>
4.3 KiB
Channel
- Author(s): Philippe Hausler
Introduction
AsyncStream introduced a mechanism to send buffered elements from a context that doesn't use Swift concurrency into one that does. That design only addressed a portion of the potential use cases; the missing portion was the back pressure excerpted across two concurrency domains.
Proposed Solution
To achieve a system that supports back pressure and allows for the communication of more than one value from one task to another we are introducing a new type, the channel. The channel will be a reference-type asynchronous sequence with an asynchronous sending capability that awaits the consumption of iteration. Each value sent by the channel, or finish transmitted, will await the consumption of that value or event by iteration. That awaiting behavior will allow for the affordance of back pressure applied from the consumption site to be transmitted to the production site. This means that the rate of production cannot exceed the rate of consumption, and that the rate of consumption cannot exceed the rate of production.
Detailed Design
Similar to the AsyncStream and AsyncThrowingStream types, the type for sending elements via back pressure will come in two versions. These two versions will account for the throwing nature or non-throwing nature of the elements being produced.
Each type will have functions to send elements and to send terminal events.
public final class AsyncChannel<Element: Sendable>: AsyncSequence, Sendable {
public struct Iterator: AsyncIteratorProtocol, Sendable {
public mutating func next() async -> Element?
}
public init(element elementType: Element.Type = Element.self)
public func send(_ element: Element) async
public func finish() async
public func makeAsyncIterator() -> Iterator
}
public final class AsyncThrowingChannel<Element: Sendable, Failure: Error>: AsyncSequence, Sendable {
public struct Iterator: AsyncIteratorProtocol, Sendable {
public mutating func next() async throws -> Element?
}
public init(element elementType: Element.Type = Element.self, failure failureType: Failure.Type = Failure.self)
public func send(_ element: Element) async
public func fail(_ error: Error) async where Failure == Error
public func finish() async
public func makeAsyncIterator() -> Iterator
}
Channels are intended to be used as communication types between tasks. Particularly when one task produces values and another task consumes said values. The back pressure applied by send(_:), fail(_:) and finish() via the suspension/resume ensure that the production of values does not exceed the consumption of values from iteration. Each of these methods suspend after enqueuing the event and are resumed when the next call to next() on the Iterator is made.
let channel = AsyncChannel<String>()
Task {
while let resultOfLongCalculation = doLongCalculations() {
await channel.send(resultOfLongCalculation)
}
await channel.finish()
}
for await calculationResult in channel {
print(calculationResult)
}
The example above uses a task to perform intense calculations; each of which are sent to the other task via the send(_:) method. That call to send(_:) returns when the next iteration of the channel is invoked.
Alternatives Considered
The use of the name "subject" was considered, due to its heritage as a name for a sync-to-async adapter type.
It was considered to make AsyncChannel and AsyncThrowingChannel actors, however due to the cancellation internals it would imply that these types would need to create new tasks to handle cancel events. The advantages of an actor in this particular case did not outweigh the impact of adjusting the implementations to be actors.
Credits/Inspiration
AsyncChannel and AsyncThrowingChannel was heavily inspired from Subject but with the key difference that it uses Swift concurrency to apply back pressure.