/rust/registry/src/index.crates.io-1949cf8c6b5b557f/backoff-0.4.0/src/backoff.rs
Line | Count | Source |
1 | | use std::time::Duration; |
2 | | |
3 | | /// `Backoff` is a backoff policy for retrying an operation. |
4 | | pub trait Backoff { |
5 | | /// Resets the internal state to the initial value. |
6 | 0 | fn reset(&mut self) {} |
7 | | /// next_backoff() time is elapsed before it is called again. |
8 | | /// If it returns None, it means the operation timed out and no |
9 | | /// further retries are done. |
10 | | fn next_backoff(&mut self) -> Option<Duration>; |
11 | | } |
12 | | |
13 | | impl<B: Backoff + ?Sized> Backoff for Box<B> { |
14 | 0 | fn next_backoff(&mut self) -> Option<Duration> { |
15 | 0 | let this: &mut B = self; |
16 | 0 | this.next_backoff() |
17 | 0 | } |
18 | | |
19 | 0 | fn reset(&mut self) { |
20 | 0 | let this: &mut B = self; |
21 | 0 | this.reset() |
22 | 0 | } |
23 | | } |
24 | | |
25 | | /// Immediately retry the operation. |
26 | | #[derive(Debug)] |
27 | | pub struct Zero {} |
28 | | |
29 | | impl Backoff for Zero { |
30 | 0 | fn next_backoff(&mut self) -> Option<Duration> { |
31 | 0 | Some(Duration::default()) |
32 | 0 | } |
33 | | } |
34 | | |
35 | | /// The operation should never be retried. |
36 | | #[derive(Debug)] |
37 | | pub struct Stop {} |
38 | | |
39 | | impl Backoff for Stop { |
40 | 0 | fn next_backoff(&mut self) -> Option<Duration> { |
41 | 0 | None |
42 | 0 | } |
43 | | } |
44 | | |
45 | | /// Contant is a backoff policy which always returns |
46 | | /// a constant duration. |
47 | | #[derive(Debug)] |
48 | | pub struct Constant { |
49 | | interval: Duration, |
50 | | } |
51 | | |
52 | | impl Constant { |
53 | | /// Creates a new Constant backoff with `interval` contant |
54 | | /// backoff. |
55 | 0 | pub fn new(interval: Duration) -> Constant { |
56 | 0 | Constant { interval } |
57 | 0 | } |
58 | | } |
59 | | |
60 | | impl Backoff for Constant { |
61 | 0 | fn next_backoff(&mut self) -> Option<Duration> { |
62 | 0 | Some(self.interval) |
63 | 0 | } |
64 | | } |