-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tokio-stream: add wrapper for broadcast and watch (#3384)
- Loading branch information
Showing
5 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
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, RecvError>, Receiver<T>)>, | ||
} | ||
|
||
/// An error returned from the inner stream of a [`BroadcastStream`]. | ||
#[derive(Debug, PartialEq)] | ||
pub enum BroadcastStreamRecvError { | ||
/// 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), | ||
} | ||
|
||
async fn make_future<T: Clone>(mut rx: Receiver<T>) -> (Result<T, RecvError>, Receiver<T>) { | ||
let result = rx.recv().await; | ||
(result, rx) | ||
} | ||
|
||
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>> { | ||
let (result, rx) = ready!(self.inner.poll(cx)); | ||
self.inner.set(make_future(rx)); | ||
match result { | ||
Ok(item) => Poll::Ready(Some(Ok(item))), | ||
Err(RecvError::Closed) => Poll::Ready(None), | ||
Err(RecvError::Lagged(n)) => { | ||
Poll::Ready(Some(Err(BroadcastStreamRecvError::Lagged(n)))) | ||
} | ||
} | ||
} | ||
} | ||
|
||
impl<T> fmt::Debug for BroadcastStream<T> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.debug_struct("BroadcastStream").finish() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
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<(), RecvError>, Receiver<T>)>, | ||
} | ||
|
||
async fn make_future<T: Clone + Send + Sync>( | ||
mut rx: Receiver<T>, | ||
) -> (Result<(), RecvError>, Receiver<T>) { | ||
let result = rx.changed().await; | ||
(result, rx) | ||
} | ||
|
||
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>> { | ||
let (result, rx) = ready!(self.inner.poll(cx)); | ||
match result { | ||
Ok(_) => { | ||
let received = (*rx.borrow()).clone(); | ||
self.inner.set(make_future(rx)); | ||
Poll::Ready(Some(received)) | ||
} | ||
Err(_) => { | ||
self.inner.set(make_future(rx)); | ||
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() | ||
} | ||
} |