1pub mod policy;
2pub mod stream;
3
4use std::{future::Future, time::Duration};
5
6pub use stream::Stream;
7
8pub enum Decision {
9 Retry(Duration),
10 Break,
11}
12
13pub trait Policy<Response> {
14 fn decide(&mut self, response: &Response) -> Decision;
15
16 fn retry<Request, S>(
17 mut self,
18 stream: S,
19 request: Request,
20 ) -> impl Future<Output = S::Response> + Send
21 where
22 Response: Send,
23 Request: Clone + Send,
24 S: Stream<Request, Response = Response> + Send,
25 <S as Stream<Request>>::Function: Send,
26 Self: Sized + Send,
27 {
28 async move {
29 loop {
30 let response = stream.next(request.clone()).await;
31 match self.decide(&response) {
32 Decision::Retry(delay) => tokio::time::sleep(delay).await,
33 Decision::Break => break response,
34 }
35 }
36 }
37 }
38}