Provide abstraction for cold streams
See original GitHub issueAll the currently provided channel abstractions in kotlinx.coroutines
are hot. The data is being produced regardless of the presence of subscriber. This is good for data sources and applications that are inherently hot, like incoming network and UI-events.
However, hot streams are not an ideal solution for cases where data stream is produced on demand. Consider, for example, the following simple code that produces ReceiveChannel<Int>
:
produce<Int> {
while (true) {
val x = computeNextValue()
send(x)
}
}
One obvious downside is the computeNextValue()
is invoked before send
, so even when receiver is not ready, the next value gets computed. Of course, it gets suspended in send
if there is no receiver, but it is not as lazy as you get with cold reactive Publisher/Observable/Flowable/Flux/Flow.
We need the abstraction for cold streams in kotlinx.coroutines
that is going to be just as lazy, computing data in “push” mode versus “pull” mode of hot channels that we have now.
There are the following related discussions:
- https://github.com/reactor/reactor-core/issues/979#issuecomment-351770494 describes preliminary performance test that indicates that “push” mode is much faster for same-thread cases.
- https://github.com/Kotlin/kotlinx.coroutines/issues/113 (SPSC channels) seems to get superseded by the support of cold streams.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:91
- Comments:116 (78 by maintainers)
Top GitHub Comments
We are actively working on it. We have finished the design phase and resolved most of the issues related to API surface and mental model; now we are prototyping it, the first eap build will arrive at the beginning of April (though I am not sure this one will be publicly announced and merged into upstream as “ready to use” feature)
It had changed somewhat:
Flow<T>
Flow<T>
, but provide.asFlow()
converters for all the appropriate types instead.