Skip to content

Commit

Permalink
Request elements in batches in ReactiveFlow to avoid requesting eleme…
Browse files Browse the repository at this point in the history
…nts one by one in a default configuration

Also, partially masks #1766
  • Loading branch information
qwwdfsad committed Jan 28, 2020
1 parent 642989e commit 6810745
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
13 changes: 10 additions & 3 deletions reactive/kotlinx-coroutines-reactive/src/ReactiveFlow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ import kotlin.coroutines.*
* Transforms the given reactive [Publisher] into [Flow].
* Use [buffer] operator on the resulting flow to specify the size of the backpressure.
* More precisely, it specifies the value of the subscription's [request][Subscription.request].
* `1` is used by default.
* [buffer] default capacity is used by default.
*
* If any of the resulting flow transformations fails, subscription is immediately cancelled and all in-flights elements
* If any of the resulting flow transformations fails, subscription is immediately cancelled and all in-flight elements
* are discarded.
*
* This function is integrated with `ReactorContext` from `kotlinx-coroutines-reactor` module,
Expand All @@ -40,16 +40,23 @@ public fun <T : Any> Flow<T>.asPublisher(): Publisher<T> = FlowAsPublisher(this)
private class PublisherAsFlow<T : Any>(
private val publisher: Publisher<T>,
context: CoroutineContext = EmptyCoroutineContext,
capacity: Int = 1
capacity: Int = Channel.BUFFERED
) : ChannelFlow<T>(context, capacity) {
override fun create(context: CoroutineContext, capacity: Int): ChannelFlow<T> =
PublisherAsFlow(publisher, context, capacity)

/*
* Suppress for Channel.CHANNEL_DEFAULT_CAPACITY.
* It's too counter-intuitive to be public and moving it to Flow companion
* will also create undesired effect.
*/
@Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
private val requestSize: Long
get() = when (capacity) {
Channel.CONFLATED -> Long.MAX_VALUE // request all and conflate incoming
Channel.RENDEZVOUS -> 1L // need to request at least one anyway
Channel.UNLIMITED -> Long.MAX_VALUE // reactive streams way to say "give all" must be Long.MAX_VALUE
Channel.BUFFERED -> Channel.CHANNEL_DEFAULT_CAPACITY.toLong()
else -> capacity.toLong().also { check(it >= 1) }
}

Expand Down
40 changes: 38 additions & 2 deletions reactive/kotlinx-coroutines-reactive/test/PublisherAsFlowTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,43 @@ class PublisherAsFlowTest : TestBase() {
expect(6)
}

publisher.asFlow().buffer(1).collect {
expect(it)
}

finish(8)
}

@Test
fun testBufferSizeDefault() = runTest {
val publisher = publish(currentDispatcher()) {
repeat(64) {
send(it + 1)
expect(it + 1)
}
assertFalse { offer(-1) }
}

publisher.asFlow().collect {
expect(64 + it)
}

finish(129)
}

@Test
fun testDefaultCapacityIsProperlyOverwritten() = runTest {
val publisher = publish(currentDispatcher()) {
expect(1)
send(3)
expect(2)
send(5)
expect(4)
send(7)
expect(6)
}

publisher.asFlow().flowOn(wrapperDispatcher()).buffer(1).collect {
expect(it)
}

Expand Down Expand Up @@ -126,7 +162,7 @@ class PublisherAsFlowTest : TestBase() {
else -> expectUnreached()
}
}
}.asFlow()
}.asFlow().buffer(1)
assertFailsWith<TestException> {
coroutineScope {
expect(2)
Expand All @@ -145,4 +181,4 @@ class PublisherAsFlowTest : TestBase() {
}
finish(6)
}
}
}

0 comments on commit 6810745

Please sign in to comment.