Skip to content

Commit b591fc6

Browse files
committed
Changed semantics of throttle to non-dropping variant with backpressure
1 parent 14d7d3b commit b591fc6

File tree

3 files changed

+67
-25
lines changed

3 files changed

+67
-25
lines changed

examples/throttle.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//! Spawns a timed task which gets throttled.
2+
3+
fn main() {
4+
#[cfg(feature = "unstable")]
5+
{
6+
use async_std::prelude::*;
7+
use async_std::task;
8+
9+
task::block_on(async {
10+
use async_std::stream;
11+
use std::time::Duration;
12+
13+
// emit value every 1 second
14+
let s = stream::interval(Duration::from_nanos(1000000)).enumerate();
15+
16+
// throttle for 2 seconds
17+
let s = s.throttle(Duration::from_secs(2));
18+
19+
s.for_each(|(n, _)| {
20+
dbg!(n);
21+
})
22+
.await;
23+
// => 0 .. 1 .. 2 .. 3
24+
// with a pause of 2 seconds between each print
25+
})
26+
}
27+
}

src/stream/stream/mod.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ use std::time::Duration;
117117
cfg_unstable! {
118118
use std::future::Future;
119119
use std::pin::Pin;
120-
use std::time::Duration;
121120

122121
use crate::stream::into_stream::IntoStream;
123122
use crate::stream::{FromStream, Product, Sum};
@@ -316,7 +315,33 @@ extension_trait! {
316315
TakeWhile::new(self, predicate)
317316
}
318317

319-
fn throttle(self, d: Duration) -> Throttle<Self, Self::Item>
318+
#[doc = r#"
319+
Limit the amount of items yielded per timeslice in a stream.
320+
321+
# Examples
322+
```ignore
323+
# fn main() { async_std::task::block_on(async {
324+
#
325+
use async_std::stream;
326+
use std::time::Duration;
327+
328+
// emit value every 1 second
329+
let s = stream::interval(Duration::from_nanos(1000000)).enumerate();
330+
331+
// throttle for 2 seconds
332+
let s = s.throttle(Duration::from_secs(2));
333+
334+
s.for_each(|(n, _)| {
335+
dbg!(n);
336+
})
337+
.await;
338+
// => 0 .. 1 .. 2 .. 3
339+
// with a pause of 2 seconds between each print
340+
#
341+
# }) }
342+
```
343+
"#]
344+
fn throttle(self, d: Duration) -> Throttle<Self>
320345
where
321346
Self: Sized,
322347
{

src/stream/stream/throttle.rs

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,60 +7,50 @@ use futures_timer::Delay;
77
use crate::stream::Stream;
88
use crate::task::{Context, Poll};
99

10-
/// A stream that only yields one element once every `duration`, and drops all others.
10+
/// A stream that only yields one element once every `duration`, and applies backpressure. Does not drop any elements.
1111
/// #[doc(hidden)]
1212
#[allow(missing_debug_implementations)]
13-
pub struct Throttle<S, T> {
13+
pub struct Throttle<S> {
1414
stream: S,
1515
duration: Duration,
1616
delay: Option<Delay>,
17-
last: Option<T>,
1817
}
1918

20-
impl<S: Unpin, T> Unpin for Throttle<S, T> {}
19+
impl<S: Unpin> Unpin for Throttle<S> {}
2120

22-
impl<S: Stream> Throttle<S, S::Item> {
21+
impl<S: Stream> Throttle<S> {
2322
pin_utils::unsafe_pinned!(stream: S);
2423
pin_utils::unsafe_unpinned!(duration: Duration);
2524
pin_utils::unsafe_pinned!(delay: Option<Delay>);
26-
pin_utils::unsafe_unpinned!(last: Option<S::Item>);
2725

2826
pub(super) fn new(stream: S, duration: Duration) -> Self {
2927
Throttle {
3028
stream,
3129
duration,
3230
delay: None,
33-
last: None,
3431
}
3532
}
3633
}
3734

38-
impl<S: Stream> Stream for Throttle<S, S::Item> {
35+
impl<S: Stream> Stream for Throttle<S> {
3936
type Item = S::Item;
4037

4138
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<S::Item>> {
4239
if let Some(d) = self.as_mut().delay().as_pin_mut() {
4340
if d.poll(cx).is_ready() {
44-
if let Some(v) = self.as_mut().last().take() {
45-
// Sets last to None.
46-
*self.as_mut().delay() = Some(Delay::new(self.duration));
47-
return Poll::Ready(Some(v));
48-
} else {
49-
*self.as_mut().delay() = None;
50-
}
41+
*self.as_mut().delay() = None;
42+
} else {
43+
return Poll::Pending;
5144
}
5245
}
5346

5447
match self.as_mut().stream().poll_next(cx) {
55-
Poll::Pending => Poll::Pending,
56-
Poll::Ready(None) => return Poll::Ready(None),
48+
Poll::Pending => {
49+
cx.waker().wake_by_ref(); // Continue driving even though emitting Pending
50+
Poll::Pending
51+
}
52+
Poll::Ready(None) => Poll::Ready(None),
5753
Poll::Ready(Some(v)) => {
58-
if self.as_mut().delay().is_some() {
59-
*self.as_mut().last() = Some(v);
60-
cx.waker().wake_by_ref(); // Continue driving even though emitting Pending
61-
return Poll::Pending;
62-
}
63-
6454
*self.as_mut().delay() = Some(Delay::new(self.duration));
6555
Poll::Ready(Some(v))
6656
}

0 commit comments

Comments
 (0)