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

Consider implementing a way to try_send without consuming the message #82

Open
let4be opened this issue Jun 9, 2021 · 4 comments
Open

Comments

@let4be
Copy link

let4be commented Jun 9, 2021

I find myself in a situation where I want to try to send a message in a channel and if buffer is full I want to send it to another "overflow" channel
Right now it's impossible to do without wasteful cloning because try_send consumes the message

This can be done either by returning original message in Result's Err when there was an error or by implementing a similar Permit API as in tokio::sync::mpsc
see https://docs.rs/tokio/1.0.1/tokio/sync/mpsc/struct.Permit.html

I'm personally in favor of Permit API as if done right it would allow to do other things, like getting a bunch of Permits(or one bigger Permit, depending on how we implement it) and sending a bunch of messages in one go without waiting
It could probably make sending multiple messages in one go more efficient

On the other hand I could wrap the message into Arc this would solve wastefulness of the cloning I do
But the Permit API, similar to tokio's could be useful for other things I described

@zesterer
Copy link
Owner

This is already supported!

https://docs.rs/flume/0.10.5/flume/struct.Sender.html#method.try_send

The function returns a Result, with TrySendError<T> as the Err variant, allowing you to get back the value. For example:

match tx.try_send(foo) {
    Ok(()) => /* Successfully sent */,
    Err(TrySendError::Full(foo)) | Err(TrySendError::Disconnected(foo)) => /* Failed to send */,
}

I could probably make this easier with an fn into_inner(self) -> T on TrySendError though. I'll add this to the next release!

@let4be
Copy link
Author

let4be commented Jun 10, 2021

Um... This is great news!

What do you think about Permit API?
Sometimes I find myself in a situation where I need to write en-bulk and all items are already there(no need to wait for), could such use case be made faster with Permits?

@zesterer
Copy link
Owner

Thinking about this carefully, I think this would be possible to implement: but not without adding some overhead to the crate. I might be able to hide it behind a non-default feature to ensure that most crate users don't need to pay the overhead, however.

That said, I'm not sure how this would interact with the async feature: the implementation could end up getting quite complex.

For this reason, I'm reluctant to do this at the current time. I'm actually working on a complete implementation overhaul of the crate that might make implementing this significantly easier, however. If I end up implementing this, I'll let you know.

@zesterer
Copy link
Owner

zesterer commented Jun 10, 2021

As a point of interest, the into_inner method was added in c13d0a4 and is now in version 0.10.6.

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

No branches or pull requests

2 participants