-
-
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.
time: add
StreamExt::chunks_timeout
(#4695)
- Loading branch information
Showing
3 changed files
with
226 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
use crate::stream_ext::Fuse; | ||
use crate::Stream; | ||
use tokio::time::{sleep, Instant, Sleep}; | ||
|
||
use core::future::Future; | ||
use core::pin::Pin; | ||
use core::task::{Context, Poll}; | ||
use pin_project_lite::pin_project; | ||
use std::time::Duration; | ||
|
||
pin_project! { | ||
/// Stream returned by the [`chunks_timeout`](super::StreamExt::chunks_timeout) method. | ||
#[must_use = "streams do nothing unless polled"] | ||
#[derive(Debug)] | ||
pub struct ChunksTimeout<S: Stream> { | ||
#[pin] | ||
stream: Fuse<S>, | ||
#[pin] | ||
deadline: Sleep, | ||
duration: Duration, | ||
items: Vec<S::Item>, | ||
cap: usize, // https://github.com/rust-lang/futures-rs/issues/1475 | ||
} | ||
} | ||
|
||
impl<S: Stream> ChunksTimeout<S> { | ||
pub(super) fn new(stream: S, max_size: usize, duration: Duration) -> Self { | ||
ChunksTimeout { | ||
stream: Fuse::new(stream), | ||
deadline: sleep(duration), | ||
duration, | ||
items: Vec::with_capacity(max_size), | ||
cap: max_size, | ||
} | ||
} | ||
} | ||
|
||
impl<S: Stream> Stream for ChunksTimeout<S> { | ||
type Item = Vec<S::Item>; | ||
|
||
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> { | ||
let mut me = self.as_mut().project(); | ||
loop { | ||
match me.stream.as_mut().poll_next(cx) { | ||
Poll::Pending => break, | ||
Poll::Ready(Some(item)) => { | ||
if me.items.is_empty() { | ||
me.deadline.as_mut().reset(Instant::now() + *me.duration); | ||
me.items.reserve_exact(*me.cap); | ||
} | ||
me.items.push(item); | ||
if me.items.len() >= *me.cap { | ||
return Poll::Ready(Some(std::mem::take(me.items))); | ||
} | ||
} | ||
Poll::Ready(None) => { | ||
// Returning Some here is only correct because we fuse the inner stream. | ||
let last = if me.items.is_empty() { | ||
None | ||
} else { | ||
Some(std::mem::take(me.items)) | ||
}; | ||
|
||
return Poll::Ready(last); | ||
} | ||
} | ||
} | ||
|
||
if !me.items.is_empty() { | ||
ready!(me.deadline.poll(cx)); | ||
return Poll::Ready(Some(std::mem::take(me.items))); | ||
} | ||
|
||
Poll::Pending | ||
} | ||
|
||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
let chunk_len = if self.items.is_empty() { 0 } else { 1 }; | ||
let (lower, upper) = self.stream.size_hint(); | ||
let lower = (lower / self.cap).saturating_add(chunk_len); | ||
let upper = upper.and_then(|x| x.checked_add(chunk_len)); | ||
(lower, upper) | ||
} | ||
} |
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,84 @@ | ||
#![warn(rust_2018_idioms)] | ||
#![cfg(all(feature = "time", feature = "sync", feature = "io-util"))] | ||
|
||
use tokio::time; | ||
use tokio_stream::{self as stream, StreamExt}; | ||
use tokio_test::assert_pending; | ||
use tokio_test::task; | ||
|
||
use futures::FutureExt; | ||
use std::time::Duration; | ||
|
||
#[tokio::test(start_paused = true)] | ||
async fn usage() { | ||
let iter = vec![1, 2, 3].into_iter(); | ||
let stream0 = stream::iter(iter); | ||
|
||
let iter = vec![4].into_iter(); | ||
let stream1 = | ||
stream::iter(iter).then(move |n| time::sleep(Duration::from_secs(3)).map(move |_| n)); | ||
|
||
let chunk_stream = stream0 | ||
.chain(stream1) | ||
.chunks_timeout(4, Duration::from_secs(2)); | ||
|
||
let mut chunk_stream = task::spawn(chunk_stream); | ||
|
||
assert_pending!(chunk_stream.poll_next()); | ||
time::advance(Duration::from_secs(2)).await; | ||
assert_eq!(chunk_stream.next().await, Some(vec![1, 2, 3])); | ||
|
||
assert_pending!(chunk_stream.poll_next()); | ||
time::advance(Duration::from_secs(2)).await; | ||
assert_eq!(chunk_stream.next().await, Some(vec![4])); | ||
} | ||
|
||
#[tokio::test(start_paused = true)] | ||
async fn full_chunk_with_timeout() { | ||
let iter = vec![1, 2].into_iter(); | ||
let stream0 = stream::iter(iter); | ||
|
||
let iter = vec![3].into_iter(); | ||
let stream1 = | ||
stream::iter(iter).then(move |n| time::sleep(Duration::from_secs(1)).map(move |_| n)); | ||
|
||
let iter = vec![4].into_iter(); | ||
let stream2 = | ||
stream::iter(iter).then(move |n| time::sleep(Duration::from_secs(3)).map(move |_| n)); | ||
|
||
let chunk_stream = stream0 | ||
.chain(stream1) | ||
.chain(stream2) | ||
.chunks_timeout(3, Duration::from_secs(2)); | ||
|
||
let mut chunk_stream = task::spawn(chunk_stream); | ||
|
||
assert_pending!(chunk_stream.poll_next()); | ||
time::advance(Duration::from_secs(2)).await; | ||
assert_eq!(chunk_stream.next().await, Some(vec![1, 2, 3])); | ||
|
||
assert_pending!(chunk_stream.poll_next()); | ||
time::advance(Duration::from_secs(2)).await; | ||
assert_eq!(chunk_stream.next().await, Some(vec![4])); | ||
} | ||
|
||
#[tokio::test] | ||
#[ignore] | ||
async fn real_time() { | ||
let iter = vec![1, 2, 3, 4].into_iter(); | ||
let stream0 = stream::iter(iter); | ||
|
||
let iter = vec![5].into_iter(); | ||
let stream1 = | ||
stream::iter(iter).then(move |n| time::sleep(Duration::from_secs(5)).map(move |_| n)); | ||
|
||
let chunk_stream = stream0 | ||
.chain(stream1) | ||
.chunks_timeout(3, Duration::from_secs(2)); | ||
|
||
let mut chunk_stream = task::spawn(chunk_stream); | ||
|
||
assert_eq!(chunk_stream.next().await, Some(vec![1, 2, 3])); | ||
assert_eq!(chunk_stream.next().await, Some(vec![4])); | ||
assert_eq!(chunk_stream.next().await, Some(vec![5])); | ||
} |