Skip to content
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

disconnect receiving side of a channel but consume objects already pushed to the channel #861

Open
behzadnouri opened this issue Jul 8, 2022 · 1 comment

Comments

@behzadnouri
Copy link

behzadnouri commented Jul 8, 2022

Lets say during the process shutdown I want:

  • The consumer thread process all items already pushed to the channel.
  • The producer thread stops pushing new items to the channel.

This requires an api which "atomically":

  • disconnects the receiving side of a channel (so that the producer can no longer push new items on to the channel).
  • but also returns all items which were already pushed on the channel before disconnect happens.

Something like:

impl Receiver<T> {
  fn consume(self) -> Vec<T> {
    // disconnect and return existing items.
  }
}

Does corssbeam channels have any such api that I am missing?
or, any paradigms to achieve the same functionality?

Currently what I am doing is something like this on the consumer side:

// start of shutdown process.
for item in receiver.try_iter() {
  // process item
}
drop(receiver);

Obviously this leaves some race condition between the time .try_iter() iterator returns None until drop(receiver) is invoked, where the producer may push new items into the channel.

We can send an out-of-band exit signal to producer channel using an Arc<AtomicBool> for example, but that is not ideal; because it fragments the data plane and control plane and leads to ugly code.

@ryoqun
Copy link
Contributor

ryoqun commented Feb 6, 2023

@behzadnouri hey, colleagues meet at upstream crate land. :) I've just submitted a pr exactly for this: #959

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants