Skip to content

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

Description

@behzadnouri

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions