rustfs_utils/retry.rs
1// Copyright 2024 RustFS Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::time::Duration;
16
17pub const MAX_RETRY: i64 = 10;
18pub const MAX_JITTER: f64 = 1.0;
19pub const NO_JITTER: f64 = 0.0;
20
21/*
22struct Delay {
23 when: Instant,
24}
25
26impl Future for Delay {
27 type Output = &'static str;
28
29 fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>)
30 -> Poll<&'static str>
31 {
32 if Instant::now() >= self.when {
33 println!("Hello world");
34 Poll::Ready("done")
35 } else {
36 // Ignore this line for now.
37 cx.waker().wake_by_ref();
38 Poll::Pending
39 }
40 }
41}
42
43struct RetryTimer {
44 rem: usize,
45 delay: Delay,
46}
47
48impl RetryTimer {
49 fn new() -> Self {
50 Self {
51 rem: 3,
52 delay: Delay { when: Instant::now() }
53 }
54 }
55}
56
57impl Stream for RetryTimer {
58 type Item = ();
59
60 fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>)
61 -> Poll<Option<()>>
62 {
63 if self.rem == 0 {
64 // No more delays
65 return Poll::Ready(None);
66 }
67
68 match Pin::new(&mut self.delay).poll(cx) {
69 Poll::Ready(_) => {
70 let when = self.delay.when + Duration::from_millis(10);
71 self.delay = Delay { when };
72 self.rem -= 1;
73 Poll::Ready(Some(()))
74 }
75 Poll::Pending => Poll::Pending,
76 }
77 }
78}*/
79
80pub fn new_retry_timer(_max_retry: i32, _base_sleep: Duration, _max_sleep: Duration, _jitter: f64) -> Vec<i32> {
81 todo!();
82}