Different Kinds of Channels in Kotlin
Overview of the different kinds of Kotlin channels and their behaviors.
Rendezvous
In Rendezvous channels capacity is 0. Which means the channel has no buffer at all. Elements are transferred only when sender and receiver meet. Which is literally what Rendezvous means. I like to picture it as a relay race where the runners need to meet at one point to pass the baton.
In technical terms this means that send
suspends until another coroutine invokes receive
, and receive
suspends until another coroutine invokes send
.
Buffered
Buffered channels have a positive capacity but are not Unlimited
. Calling send
suspends only if the buffer is full. And calling receive
suspends only if buffer is empty (i.e. there are no more messages in the channel).
Unlimited
You guessed it. Unlimited buffer. Sender will never suspend on send
.
But there’s no such thing as Unlimited, right? The implementation uses a linked-list buffer so your only constraint is memory.
Conflated
This is the oddball. The sender never suspends, but the channel offers at most one element at any given time. When a new element comes, the previous element in the channel (if any) is discarded. The receiver only gets the most recent element sent. Previous elements are lost.