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

sync: remove try_recv() from mpsc types #3263

Merged
merged 3 commits into from Dec 14, 2020
Merged

sync: remove try_recv() from mpsc types #3263

merged 3 commits into from Dec 14, 2020

Conversation

carllerche
Copy link
Member

The mpsc try_recv() functions have an issue where a sent message
happens-before a call to try_recv() but try_recv() returns None.
Fixing this is non-trivial, so the function is removed for 1.0. When the
bug is fixed, the function can be added back.

Closes #2020

The mpsc `try_recv()` functions have an issue where a sent message
happens-before a call to `try_recv()` but `try_recv()` returns `None`.
Fixing this is non-trivial, so the function is removed for 1.0. When the
bug is fixed, the function can be added back.

Closes #2020
@carllerche carllerche added this to the v1.0 milestone Dec 12, 2020
@Darksonn Darksonn added A-tokio Area: The main tokio crate M-sync Module: tokio/sync labels Dec 13, 2020
@LinusCDE
Copy link

Would be nice to have a replacement for this. Trying to migrate to 1.X and this was one of my roadblocks.

I'm using UnboundedReceiver as a means to "listen to events". The data sent is already timestamped, since a accurate time was needed anyways.

(One usecase has me basically polling on this receiver to summarize it's data in a regular interval. The context is async, but since I need to access some shared state I used a 200ms interval to reduce potential mutex locks in case a lot of small data packets arrive.)

I currently don't see a trivial way to drain a Receiver of all new messages without waiting for the next item.

I'll probably use a dirty workaround for the time being and look into a better overall fix using timeout more wholeheartedly.

// Read all available messages at once
- while let Ok(event) = event_receiver.try_recv() {
- // ...
- }
+ let started = std::time::Instant::now();
+ while let Ok(Some(event)) = tokio::time::timeout(Duration::from_millis(50), event_receiver.recv()).await {
+ // Interrupt this loop after at least 100 millis to prevent unlimited looping in edge cases
+ if started.elapsed() > Duration::from_millis(100) {
+     break;
+ }
+ // ...
+ }

This may change my polling interval from 200ms to 349.9ms, but this works for me for the time being.

@Darksonn
Copy link
Contributor

@LinusCDE Is there any reason you aren't using the workaround described in #3350?

@LinusCDE
Copy link

@LinusCDE Is there any reason you aren't using the workaround described in #3350?

I didn't know about that solution. This is great! Thanks for making me aware of that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate M-sync Module: tokio/sync
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants