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

Add force_send method to channel Sender #1135

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bergkvist
Copy link

@bergkvist bergkvist commented Sep 13, 2024

Add a force_push method to the Sender part of a channel.

In particular, this is useful for bounded channels of non-zero size.

Motivation:

Design goals:

  • It is fine to lose messages, but messages should never appear in a different order from which they were sent
  • For bounded (non-zero-sized) channels, force_send should never fail (except when the receiver has been dropped)
  • Should not be blocking
use crossbeam_channel::{bounded, ForceSendError};

let (s, r) = bounded(3);

assert_eq!(s.force_send(0), Ok(None));
assert_eq!(s.force_send(1), Ok(None));
assert_eq!(s.force_send(2), Ok(None));
assert_eq!(s.force_send(3), Ok(Some(2)));

assert_eq!(r.recv(), Ok(0));

assert_eq!(s.force_send(4), Ok(None));

assert_eq!(r.recv(), Ok(1));
assert_eq!(r.recv(), Ok(3));

assert_eq!(s.force_send(5), Ok(None));
assert_eq!(s.force_send(6), Ok(None));
assert_eq!(s.force_send(7), Ok(Some(6)));
assert_eq!(s.force_send(8), Ok(Some(7)));

assert_eq!(r.recv(), Ok(4));
assert_eq!(r.recv(), Ok(5));
assert_eq!(r.recv(), Ok(8));

drop(r);

assert_eq!(s.force_send(9), Err(ForceSendError::Disconnected(9)));

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

Successfully merging this pull request may close these issues.

1 participant