Skip to content

Request elements in batches in ReactiveFlow to avoid requesting eleme… #1778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}