zesterer / flume

A safe and fast multi-producer, multi-consumer channel.
https://crates.io/crates/flume
Apache License 2.0
2.47k stars 85 forks source link

Add a `force_send()` function to `flume::Sender`? #142

Open BTOdell opened 8 months ago

BTOdell commented 8 months ago

Flume has:

  1. flume::Sender::send: block until space is available
  2. flume::Sender::send_async: block until space is available (but async)
  3. flume::Sender::try_send: attempt to send, fail otherwise

I propose adding a new function that implements "lossy ring buffer" functionality:

impl<T> Sender<T> {
    pub fn force_send(&self, value: T) -> Result<Option<T>, SendError<T>> {
        todo!()
    }
}

If the channel is bounded and full, the new value will be forcefully pushed onto the channel and the oldest value that hasn't been received yet will be returned in the Option<T>. If the channel isn't full, then None is returned.

For context, I posted a Reddit thread to ask the community for help/advice for what library to use for this functionality. Although, from my research, it seems like Flume would be a great candidate to directly add this functionality to because it has all of the other features I need! 😄

I'm going to start learning about Flume's code base so I can start formulating a PR.