use futures::{Stream, ready};
use pin_project_lite::pin_project;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;
use tokio::time::{Interval, interval};
pub trait ThrottleExt: Stream {
fn throttle(self, duration: Duration) -> Throttle<Self>
where
Self: Sized,
{
Throttle::new(self, duration)
}
}
impl<T: Stream> ThrottleExt for T {}
pin_project! {
pub struct Throttle<S: Stream> {
#[pin]
stream: S,
interval: Interval,
pending: Option<S::Item>,
}
}
impl<S: Stream> Throttle<S> {
pub fn new(stream: S, duration: Duration) -> Self {
let mut interval = interval(duration);
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
Self { stream, interval, pending: None }
}
}
impl<S: Stream> Stream for Throttle<S> {
type Item = S::Item;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let mut this = self.project();
ready!(this.interval.poll_tick(cx));
loop {
match this.stream.as_mut().poll_next(cx) {
Poll::Ready(Some(item)) => {
*this.pending = Some(item);
}
Poll::Ready(None) => {
return Poll::Ready(this.pending.take());
}
Poll::Pending => {
return Poll::Ready(this.pending.take());
}
}
}
}
}