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.

Fail-fast when trying to consume a channel multiple times

See original GitHub issue

Channels are designed to be used in fan-out fashion or in a pipeline fashion. Pipeline is conveniently performed with various operators like filter, map, etc (see PR #88) which are modeled after a Sequence. However, a sequence is a multi-shot abstraction, for the following is perfectly file:

val s: Sequence<T> = ...
s.filter { ... }.forEach { it.doSomething() }
s.map { ... }.forEach { it.doSomethingElse() }

However, a similar code will not work in the same way with a ReceiveChannel. ReceiveChannel is more like Iterator, than it is like a Sequence. Each element of the ReceiveChannel can be consumed only once. It is expected that this might be a common source of errors and misunderstanding, so the proposal is to make all the standard operators “fail-fast” when the channel is already being consumed by another operator. So, in the above example, if the types of s was to be replaced with ReceiveChannel<T>, then the invocation of s.map (which comes after s.filter) shall fail immediately.

P.S. It is a separate issue whether a truly Sequence-like (Rx-like) higher-order abstraction shall be introduced for channels.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
bdavisxcommented, Jun 21, 2018

I’m confused by the description, will consumeEach only be able to be called once? If so how would you do a fan-out?

1reaction
ZakTaccardicommented, Jun 12, 2018

just want to confirm that the following would still work:

val channel: ReceiveChannel<T> = ...

channel.filter { .. }
    .map { .. }
    .consumeEach { .. }

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fail Fast and Fail Safe Iterator in Java - Javatpoint
Instead, it tries to avoid failures as much as possible. In this section, we will discuss both the fail fast and Fail Safe...
Read more >
Idiomatic goroutine termination and error handling
I want to write a method fetchAll that queries an unspecified number of resources from remote servers in parallel. If any of the...
Read more >
Kafka - How to fail gracefully - Quarkus
The Kafka connector proposes three failure handling strategies, and that's what we are going to detail now. The "Fail-Fast" strategy. The first ...
Read more >
Chapter 4, Concurrency Patterns in Go - O'Reilly
At times you may find yourself wanting to combine one or more done channels into a single done channel that closes if any...
Read more >
An Introduction to Channels in Go (Golang)
package main import ( "fmt" ) func main() { n := 3 // This is where we "make" the channel, which can be...
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