Timeout provider as resource
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
use futures_util::Stream;
|
||||
use std::pin::Pin;
|
||||
use streams::streamtimeout::TimeoutableStream;
|
||||
#[cfg(test)]
|
||||
mod test;
|
||||
|
||||
use std::time::Duration;
|
||||
use streams::streamtimeout::BoxedTimeoutFuture;
|
||||
use streams::streamtimeout::StreamTimeout2;
|
||||
|
||||
pub struct StreamTimeout {}
|
||||
|
||||
@@ -8,16 +11,14 @@ impl StreamTimeout {
|
||||
pub fn new() -> Self {
|
||||
Self {}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> streams::streamtimeout::StreamTimeout<S> for StreamTimeout {
|
||||
fn timeout_intervals(&self, inp: Pin<Box<dyn Stream<Item = S> + Send>>) -> Pin<Box<dyn Stream<Item = S> + Send>> {
|
||||
todo!()
|
||||
pub fn boxed() -> Box<dyn StreamTimeout2> {
|
||||
Box::new(Self::new())
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> streams::streamtimeout::StreamTimeout2<S> for StreamTimeout {
|
||||
fn timeout_intervals(&self, inp: S) -> TimeoutableStream<S> {
|
||||
todo!()
|
||||
impl StreamTimeout2 for StreamTimeout {
|
||||
fn timeout_intervals(&self, ivl: Duration) -> BoxedTimeoutFuture {
|
||||
Box::pin(tokio::time::sleep(ivl))
|
||||
}
|
||||
}
|
||||
|
||||
48
crates/streamio/src/streamtimeout/test.rs
Normal file
48
crates/streamio/src/streamtimeout/test.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
use super::StreamTimeout;
|
||||
use futures_util::StreamExt;
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
use streams::streamtimeout::TimeoutableStream;
|
||||
|
||||
async fn stream_timeout_inner() -> Result<(), u8> {
|
||||
let stream = futures_util::stream::iter((0..500).collect::<Vec<_>>());
|
||||
let stream = stream.then({
|
||||
let mut i = 0;
|
||||
move |x| {
|
||||
i += 1;
|
||||
let dur = if i % 5 == 0 { 500 } else { 0 };
|
||||
async move {
|
||||
tokio::time::sleep(Duration::from_millis(dur)).await;
|
||||
x
|
||||
}
|
||||
}
|
||||
});
|
||||
let stream = stream.inspect(|_| {
|
||||
// eprintln!("A see {x:?}");
|
||||
});
|
||||
let timeout_provider = StreamTimeout::new();
|
||||
let timeout_provider = Box::new(timeout_provider);
|
||||
let ivl = Duration::from_millis(200);
|
||||
let stream = TimeoutableStream::new(ivl, timeout_provider, stream);
|
||||
let stream = stream.inspect({
|
||||
let mut tsl = Instant::now();
|
||||
move |x| match x {
|
||||
Some(x) => {
|
||||
let tsnow = Instant::now();
|
||||
let dt = tsnow.saturating_duration_since(tsl).as_secs_f32() * 1e3;
|
||||
eprintln!("B see {x:?} {dt:7.2}");
|
||||
tsl = tsnow;
|
||||
}
|
||||
None => {
|
||||
eprintln!("B see None");
|
||||
}
|
||||
}
|
||||
});
|
||||
stream.count().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn stream_timeout() {
|
||||
taskrun::run(stream_timeout_inner()).unwrap()
|
||||
}
|
||||
Reference in New Issue
Block a user