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

tokio-stream: add wrapper for broadcast and watch #3384

Merged
merged 19 commits into from
Feb 5, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2018"
# [dependencies] instead.
[dev-dependencies]
tokio = { version = "1.0.0", features = ["full", "tracing"] }
tokio-util = { version = "0.6.1", features = ["full"] }
tokio-util = { version = "0.6.3", features = ["full"] }
tokio-stream = { version = "0.1" }

async-stream = "0.3"
Expand Down
2 changes: 1 addition & 1 deletion tokio-stream/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ fs = ["tokio/fs"]
futures-core = { version = "0.3.0" }
pin-project-lite = "0.2.0"
tokio = { version = "1.0", features = ["sync"] }
tokio-util = { version = "0.6.3" }

[dev-dependencies]
tokio = { version = "1.0", features = ["full", "test-util"] }
tokio-test = { path = "../tokio-test" }
async-stream = "0.3"
futures = { version = "0.3", default-features = false }

proptest = "0.10.0"
Expand Down
7 changes: 7 additions & 0 deletions tokio-stream/src/wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ pub use mpsc_bounded::ReceiverStream;
mod mpsc_unbounded;
pub use mpsc_unbounded::UnboundedReceiverStream;

mod broadcast;
pub use broadcast::BroadcastStream;
pub use broadcast::BroadcastStreamRecvError;

mod watch;
pub use watch::WatchStream;

cfg_time! {
mod interval;
pub use interval::IntervalStream;
Expand Down
77 changes: 77 additions & 0 deletions tokio-stream/src/wrappers/broadcast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::pin::Pin;
use tokio::sync::broadcast::error::RecvError;
use tokio::sync::broadcast::Receiver;

use futures_core::Stream;
use tokio_util::sync::ReusableBoxFuture;

use std::fmt;
use std::task::{Context, Poll};

/// A wrapper around [`tokio::sync::broadcast::Receiver`] that implements [`Stream`].
///
/// [`tokio::sync::broadcast::Receiver`]: struct@tokio::sync::broadcast::Receiver
/// [`Stream`]: trait@crate::Stream
pub struct BroadcastStream<T> {
inner: ReusableBoxFuture<Result<(T, Receiver<T>), WrappedRecvError<T>>>,
}

/// An error returned from the inner stream of a [`BroadcastStream`].
#[derive(Debug, PartialEq)]
pub enum BroadcastStreamRecvError {
Darksonn marked this conversation as resolved.
Show resolved Hide resolved
/// The receiver lagged too far behind. Attempting to receive again will
/// return the oldest message still retained by the channel.
///
/// Includes the number of skipped messages.
Lagged(u64),
}

#[derive(Debug)]
Darksonn marked this conversation as resolved.
Show resolved Hide resolved
enum WrappedRecvError<T> {
Lagged(u64, Receiver<T>),
Closed,
}

async fn make_future<T: Clone>(
mut rx: Receiver<T>,
) -> Result<(T, Receiver<T>), WrappedRecvError<T>> {
match rx.recv().await {
Ok(item) => Ok((item, rx)),
Err(RecvError::Lagged(n)) => Err(WrappedRecvError::Lagged(n, rx)),
Err(RecvError::Closed) => Err(WrappedRecvError::Closed),
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to simplify to the following.

Suggested change
enum WrappedRecvError<T> {
Lagged(u64, Receiver<T>),
Closed,
}
async fn make_future<T: Clone>(
mut rx: Receiver<T>,
) -> Result<(T, Receiver<T>), WrappedRecvError<T>> {
match rx.recv().await {
Ok(item) => Ok((item, rx)),
Err(RecvError::Lagged(n)) => Err(WrappedRecvError::Lagged(n, rx)),
Err(RecvError::Closed) => Err(WrappedRecvError::Closed),
}
}
async fn make_future<T: Clone>(
mut rx: Receiver<T>,
) -> (Result<T, RecvError<T>>, Receiver<T>) {
let result = rx.recv().await;
(result, rx)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks much better 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks much better 😄


impl<T: 'static + Clone + Send> BroadcastStream<T> {
/// Create a new `BroadcastStream`.
pub fn new(rx: Receiver<T>) -> Self {
Self {
inner: ReusableBoxFuture::new(make_future(rx)),
}
}
}

impl<T: 'static + Clone + Send> Stream for BroadcastStream<T> {
type Item = Result<T, BroadcastStreamRecvError>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match ready!(self.inner.poll(cx)) {
Ok((item, rx)) => {
self.inner.set(make_future(rx));
Poll::Ready(Some(Ok(item)))
}
Err(err) => match err {
WrappedRecvError::Closed => Poll::Ready(None),
WrappedRecvError::Lagged(n, rx) => {
self.inner.set(make_future(rx));
Poll::Ready(Some(Err(BroadcastStreamRecvError::Lagged(n))))
}
},
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to call self.inner.set(make_future(rx)) in all three cases because then, if the user calls poll_next after WrappedRecvError::Closed, then we will just return Poll::Ready(None) again.

With this implementation, it will panic if polled again after the first WrappedRecvError::Closed.

You should be able to avoid duplicating the call three times by doing it before the match (but after the self.inner.poll call).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good catch. Thank you 👍

}
}

impl<T: Clone> fmt::Debug for BroadcastStream<T> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
impl<T: Clone> fmt::Debug for BroadcastStream<T> {
impl<T> fmt::Debug for BroadcastStream<T> {

fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BroadcastStream").finish()
}
}
56 changes: 56 additions & 0 deletions tokio-stream/src/wrappers/watch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::pin::Pin;
use tokio::sync::watch::Receiver;

use futures_core::Stream;
use tokio_util::sync::ReusableBoxFuture;

use std::fmt;
use std::task::{Context, Poll};
use tokio::sync::watch::error::RecvError;

/// A wrapper around [`tokio::sync::watch::Receiver`] that implements [`Stream`].
///
/// [`tokio::sync::watch::Receiver`]: struct@tokio::sync::watch::Receiver
/// [`Stream`]: trait@crate::Stream
pub struct WatchStream<T> {
inner: ReusableBoxFuture<Result<((), Receiver<T>), RecvError>>,
}

async fn make_future<T: Clone + Send + Sync>(
mut rx: Receiver<T>,
) -> Result<((), Receiver<T>), RecvError> {
let signal = rx.changed().await?;
Ok((signal, rx))
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same change here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, sorry I forgot this one.


impl<T: 'static + Clone + Unpin + Send + Sync> WatchStream<T> {
/// Create a new `WatchStream`.
pub fn new(rx: Receiver<T>) -> Self {
Self {
inner: ReusableBoxFuture::new(make_future(rx)),
}
}
}

impl<T: Clone + 'static + Send + Sync> Stream for WatchStream<T> {
type Item = T;

fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
match ready!(self.inner.poll(cx)) {
Ok((_, rx)) => {
let received = (*rx.borrow()).clone();
self.inner.set(make_future(rx));
Poll::Ready(Some(received))
}
Err(_) => Poll::Ready(None),
}
}
}

impl<T> Unpin for WatchStream<T> {}

impl<T> fmt::Debug for WatchStream<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("WatchStream").finish()
}
}