-
Notifications
You must be signed in to change notification settings - Fork 339
Implemented StreamExt::throttle #356
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a239350
Implemented StreamExt::throttle
Wassasin ced5281
Merge remote-tracking branch 'upstream/master' into 342-stream-throttle
Wassasin 1c843a8
Re-implemented Throttle to keep last value in memory
Wassasin 1fd05a1
Reset delay to prevent poll after ready
Wassasin 14d7d3b
Merge remote-tracking branch 'upstream/master' into 342-stream-throttle
Wassasin b591fc6
Changed semantics of throttle to non-dropping variant with backpressure
Wassasin 139a34b
Make throttle an unstable feature
Wassasin ef958f0
Use pin_project_lite instead for throttle
Wassasin 7c73867
Wrap around throttle comment
Wassasin 6f6d5e9
Updated throttle fn comments.
Wassasin 88cbf2c
Change throttle test to run in milliseconds
Wassasin a722de1
Merge remote-tracking branch 'upstream/master' into 342-stream-throttle
Wassasin 77a1849
Merge branch '342-stream-throttle' of github.com:Wassasin/async-std i…
Wassasin 6990c14
Reimplemented throttle to never drop Delay, added boolean flag
Wassasin 4ab7b21
Updated example to be consistent; added timing measurements to throttle
Wassasin c5b3a98
Increased throttle test to 10x time
Wassasin 90c67c2
Decreased throttle test time to original values; only test lower bound
Wassasin dda65cb
Start throttle measurement before initialisation
Wassasin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,27 @@ | ||
//! Spawns a timed task which gets throttled. | ||
|
||
fn main() { | ||
#[cfg(feature = "unstable")] | ||
{ | ||
use async_std::prelude::*; | ||
use async_std::task; | ||
|
||
task::block_on(async { | ||
use async_std::stream; | ||
use std::time::Duration; | ||
|
||
// emit value every 1 second | ||
let s = stream::interval(Duration::from_secs(1)).enumerate(); | ||
|
||
// throttle for 2 seconds | ||
let s = s.throttle(Duration::from_secs(2)); | ||
|
||
s.for_each(|(n, _)| { | ||
dbg!(n); | ||
}) | ||
.await; | ||
// => 0 .. 1 .. 2 .. 3 | ||
// with a pause of 2 seconds between each print | ||
}) | ||
} | ||
} | ||
This file contains hidden or 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 hidden or 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,70 @@ | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::time::{Duration, Instant}; | ||
|
||
use futures_timer::Delay; | ||
use pin_project_lite::pin_project; | ||
|
||
use crate::stream::Stream; | ||
use crate::task::{Context, Poll}; | ||
|
||
pin_project! { | ||
/// A stream that only yields one element once every `duration`. | ||
/// | ||
/// This `struct` is created by the [`throttle`] method on [`Stream`]. See its | ||
/// documentation for more. | ||
/// | ||
/// [`throttle`]: trait.Stream.html#method.throttle | ||
/// [`Stream`]: trait.Stream.html | ||
#[doc(hidden)] | ||
#[allow(missing_debug_implementations)] | ||
pub struct Throttle<S> { | ||
#[pin] | ||
stream: S, | ||
duration: Duration, | ||
#[pin] | ||
blocked: bool, | ||
#[pin] | ||
delay: Delay, | ||
} | ||
} | ||
|
||
impl<S: Stream> Throttle<S> { | ||
pub(super) fn new(stream: S, duration: Duration) -> Self { | ||
Throttle { | ||
stream, | ||
duration, | ||
blocked: false, | ||
delay: Delay::new(Duration::default()), | ||
} | ||
} | ||
} | ||
|
||
impl<S: Stream> Stream for Throttle<S> { | ||
type Item = S::Item; | ||
|
||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<S::Item>> { | ||
let mut this = self.project(); | ||
if *this.blocked { | ||
let d = this.delay.as_mut(); | ||
if d.poll(cx).is_ready() { | ||
*this.blocked = false; | ||
} else { | ||
return Poll::Pending; | ||
} | ||
} | ||
|
||
match this.stream.poll_next(cx) { | ||
Poll::Pending => { | ||
cx.waker().wake_by_ref(); // Continue driving even though emitting Pending | ||
Poll::Pending | ||
} | ||
Poll::Ready(None) => Poll::Ready(None), | ||
Poll::Ready(Some(v)) => { | ||
*this.blocked = true; | ||
this.delay.reset(Instant::now() + *this.duration); | ||
Poll::Ready(Some(v)) | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we could convert this to an actual test that would be ideal. I don't think it might be a bit too specific to have as a standalone example like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still don't think we quite need this example here, but I'll file a follow-up PR to do so. Thanks heaps!