When running a Channel for a long time, whats the ...
# coroutines
j
When running a Channel for a long time, whats the main difference of using Channel<Item>(Channel.BUFFERED) and Channel<Item>(Channel.UNLIMITED)? Having suspend on buffer overflow that is. Whats the actual limitation when using consumeEach? Can it has negative impact on send new values to the Channel, and in which scenarios except from Buffer default limit is 64 right?
p
The difference is back pressure. If you have an limited buffer; then the producer can detect if the consumer is unable to keep up, and will stop publishing further items. With an unlimited channel the producer will keep on producing.
j
Thanks! How is that detected and is producer blocked if called from another dispatcher? Confused when multiple places using different coroutine scope but same dispatcher like default vs io.
p
It depends on how the producer interacts with the channel. If you use
send(...)
the call will be suspended until the consumer picks up the next item. If however, if the producer uses
trySend(...)
, it will return a
ChannelResult
and the producer can check if the send was successful and act accordingly.
j
Thanks! Yeah just curious if it suspends if having negative impact if using lets say io dispatcher both in send and other places. Struggle with some odd behaviour not entirely sure if related or not.
p
I don't think suspending should have a negative impact. Once a coroutine suspended, it will hand back it's thread. I guess the only thing about the IO dispatcher is that it's the designated dispatcher for handling blocking operations. What odd behaviour are you seeing?
j
Theres a lot of "odd" behaviour, but want to make sure coroutines between multiple places not impact each other. As of example asked earlier about delay had negative impact on suspending blocks in consumeEach. Problem if multiple places hand over between coroutines in callback. In my case doing io operations inside consume each of channel and in some cases from other io operations lend over work between classes. In general no nice guidelines how and when using different Flow patterns vs Channel.