swift-async-algorithms icon indicating copy to clipboard operation
swift-async-algorithms copied to clipboard

Add a proposal for AsyncChannel

Open phausler opened this issue 3 years ago • 6 comments

This proposal formalizes the AsyncChannel and AsyncThrowingChannel types.

Read the full proposal here.

phausler avatar Oct 18 '22 21:10 phausler

Hi @phausler should we consider this https://forums.swift.org/t/asyncchannel-should-we-allow-to-buffer/60876/8 for the v1 ?

twittemb avatar Oct 19 '22 11:10 twittemb

@twittemb my initial gut reaction is that the buffering variants should be their own types, provided they need a slightly different interface as you were suggesting?

Now if they can be done with the same interface but just some extra knobs/dials on the initialization then perhaps we can add that as a 1.1 improvement?

Particularly for the channel type I want there to be a very easy on-ramp to more complex functionality; as the programmatic understanding becomes more complex the interface can grow in complexity; leaving the more simple concepts more simple to use and the advanced concepts gradually come at the cost of more advance use.

phausler avatar Oct 19 '22 15:10 phausler

@phausler I was seeing the same interface with an init(_:) taking a buffer size with a default value.

The send(_:) would still be async but the suspension would be conditioned by the state of the buffer. It will bring complexity in the internal code yes. Perhaps we could introduce an external state machine concept as we did for merge and zip to help with that.

There is still something I'm not sure of (perhaps you can tell): I'm not sure we can "not suspend" at all if the buffer has slots, or if we have to suspend/resume immediately in an atomic operation.

Let's say that we HAVE to suspend/resume immediately (because of the way concurrency is managed with ManagedCriticalState + Continuations), do we still have a benefit (performance wise) ?

twittemb avatar Oct 19 '22 16:10 twittemb

Does it make sense to have the default of the init be the 0 case as we have it?

Per the "not suspend" part: it is 100% reasonable to have an async method that conditionally suspends sometimes and not others; after all that is how the AsyncBufferedByteIterator works.

Do you have any prototypes yet of that? TBH AsyncChannel's internals can definitely be optimized (and simplified I think).

For other reasons I would like to step up our proposals to get to a 1.0 asap. So from a delivery standpoint it makes sense to have that as a future direction.

phausler avatar Oct 19 '22 16:10 phausler

I left a comment on the forums post. Maybe we can keep the discussion over there to consolidate all views.

FranzBusch avatar Oct 19 '22 17:10 FranzBusch

As a reminder @phausler, we opened this issue a while ago (https://github.com/apple/swift-async-algorithms/issues/148) because some cases were hard to test without "inspecting" the internal behaviour of Channels. The issue is tagged v1.0, so I wonder if we want to tackle that now ?

twittemb avatar Oct 20 '22 12:10 twittemb

I've added a reply to the thread -> https://forums.swift.org/t/pitch-async-buffered-channel/59854/8 regarding consistency between AsyncChannel and AsyncStream. @phausler @FranzBusch

twittemb avatar Nov 02 '22 16:11 twittemb

Hey @phausler do you have a take on the AsyncChannel / AsyncStream topic ?

twittemb avatar Nov 16 '22 20:11 twittemb

Hi @phausler can you take a look at https://github.com/apple/swift-async-algorithms/pull/235 where I suggest a new implementation for AsyncChannel?

Thanks.

twittemb avatar Nov 20 '22 23:11 twittemb

The send method doesn't return the reason for completion, and the producer have no information on what happened to the sent value. For example, yield method on AsyncStream's continuation returns an YieldResult.

Returning similar result based on the return reason:

  • cooperative cancellation
  • channel termination
  • sent value consumed

will help community to use channel building custom data structures.

soumyamahunt avatar Mar 11 '23 04:03 soumyamahunt

This proposal has been accepted. So merging this PR.

FranzBusch avatar Sep 21 '23 09:09 FranzBusch