Skip to content

[Channels] implement AsyncBufferedChannel #229

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

Closed
wants to merge 1 commit into from

Conversation

twittemb
Copy link
Contributor

@twittemb twittemb commented Nov 12, 2022

Hi.

This PR is a draft implementation of the variant to AsyncChannel mentioned in this thread -> https://forums.swift.org/t/asyncchannel-should-we-allow-to-buffer/60876/12

Here the description:

/// A channel for sending elements from one task to another.
/// The back pressure is handled by buffering values until a limit is reached
/// and then by suspending send operations until there are available slots in the buffer.
///
/// The `AsyncBufferedChannel` class is intended to be used as a communication type between tasks,
/// particularly when one task produces values and another task consumes those values.
///
/// Although the `send(_:)` function is marked `async`, it will suspend only if the internal buffer is full.
/// It will be resumed when a call to `next()` frees a slot in the buffer.
///
/// The `finish()` function marks the channel as terminated. The buffered and suspended elements
/// will remain available and dequeued on calls to `next()`.
/// In this terminal state, a call to `send(_:)` will resume immediately and the element will be discarded.

As discussed in the forum, it does not necessarely target the v1.0.

That being said, I think we can reuse the same kind of implementation (state machine / storage / async sequence) for AsyncChannel as it better separates the concerns and it is now the "default" implementation when there is a shared state to manage between producers/consumers or racing tasks.

Also, as mentioned here -> https://forums.swift.org/t/pitch-async-buffered-channel/59854/9 we might want to move away from the "all-in-one" paradigm in favour of a pair continuation/async sequence as pitched by @FranzBusch for AsyncStream. In that case the implementation is still valid by reusing the storage in both parts.

I have intentionally not implemented the throwing version waiting for more feedback from the community.

@phausler @FranzBusch I guess you are the ones that can review it when this is the right timing.

@twittemb twittemb marked this pull request as draft November 12, 2022 23:34
@twittemb twittemb closed this Nov 21, 2022
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

Successfully merging this pull request may close these issues.

1 participant