question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Implement receiveOrClosed for channels and select clause

See original GitHub issue

We need receiveOrClosed method on top of channels and onReceiveOrClosed for select which will have return type Either<E, CloseToken>.

Currently, pattern to work with channels always has the following form which is clumsy and obfuscates code:

 try {
    source.onReceive { ... }
} catch (e: ClosedReceiveChannelException) {
  // source is closed
}

The better alternative may be

source.onReceiveOrClosed {
  when (it) {
    CloseToken -> // end up consumption
    _ -> // proceed normally
  }
}

For implementation to be efficient we need inline classes (https://github.com/Kotlin/KEEP/issues/104)

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
qwwdfsadcommented, Apr 16, 2021

It will be added to coroutines 1.5.0 under receiveCatching and onReceiveCatching names.

It’s already implemented and merged, the rationale of naming is explained in this comment

2reactions
elizarovcommented, Apr 15, 2018

Let me clarify that the considerable fraction of value is going to come from support for onReceiveOrClosed clause in select expression. With that you can write operations on multiple channels with less boilerplate, without having to deal with concurrency, and with ability to code any behavior on the close of any of the source channels.

Note, that when we have channel.job.onJoin you still will NOT be able to use onJoin in select together with onReceive, e.g this:

select {
    channel.onReceive { ... }
    channel.job.onClose { ... }
}

will not work as expected. We shall make sure it fails fast in this case with a clear error message.

Another note, is that we should not actually expose CloseToken in public API, since trying to send this token over another channel will lead to unpredictable results, so we shall encapsulate it by introducing a special ValueOrClosed<T> type (that will be implement as inline class in the future).

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to safely select across channels where some may get ...
The outstanding proposal is to add receiveOrClose function and onReceiveOrClosed clause for select that would make writing code like this ...
Read more >
ReceiveChannel - Javadoc.io
Clause for select expression of ReceiveChannel.receive suspending function that selects with the element that is received from the channel.
Read more >
Select expression (experimental) - Kotlin
The onReceive clause in select fails when the channel is closed causing the corresponding select to throw an exception. We can use ......
Read more >
Fencing off go: liveness and safety for channel-based ...
This restriction allows us to implement bounded verification ... Select. The select construct in Go encodes a form of guarded.
Read more >
Diff - platform/external/kotlinx.coroutines - Google Git
+ * All operators on channels (that were prone to coroutine leaks) are deprecated in the ... + * Implemented `onTimeout` clause for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found