Selecting on close

Reading from a channel using select() is nice until it gets closed.

You can see an example of that problem here:

val p1 = produce {
repeat(10) {
send("A")
}
}

val p2 = produce {
repeat(5) {
send("B")
}
}


runBlocking {
repeat(15) {
val result = selectUnbiased<String> {
p1.onReceive {
it
}
p2.onReceive {
it
}
}

println(result)
}
}

Although the numbers add up, we may often receive ClosedReceiveChannelException running this code. That's because the second producer has fewer items, and as soon as it finishes, it will close its channel.

To avoid that, we can use onReceiveOrNull, which will return a nullable version at the same time. Once the channel gets closed, we'll receive null in our select.

We can handle this null value in any way we want, for example, by making use of the elvis operator:

repeat(15) {
val result = selectUnbiased<String> {
p1.onReceiveOrNull {
// Can throw my own exception
it ?: throw RuntimeException()
}
p2.onReceiveOrNull {
// Or supply default value
it ?: "p2 closed"
}
}

println(result)
}

Using that knowledge, we can drain both channels by skipping the null results:

var count = 0
while (count < 15) {
val result = selectUnbiased<String?> {
p1.onReceiveOrNull {
it
}
p2.onReceiveOrNull {
it
}
}

if (result != null) {
println(result)
count++
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.154.64