1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#![cfg_attr(feature = "nightly", deny(missing_docs))]
#![cfg_attr(feature = "nightly", feature(external_doc))]
#![cfg_attr(feature = "nightly", doc(include = "../README.md"))]
#![cfg_attr(test, deny(warnings))]
extern crate rand;
mod iter;
use std::time;
#[derive(Debug, Clone)]
pub struct Backoff {
retries: u32,
min: time::Duration,
max: time::Duration,
jitter: f32,
factor: u32,
}
impl Backoff {
#[inline]
pub fn new(retries: u32) -> Self {
Self {
retries,
min: time::Duration::from_millis(100),
max: time::Duration::from_secs(10),
jitter: 0.3,
factor: 2,
}
}
#[inline]
pub fn timeout_range(
mut self,
min: time::Duration,
max: time::Duration,
) -> Self {
self.min = min;
self.max = max;
self
}
#[inline]
pub fn jitter(mut self, jitter: f32) -> Self {
assert!(
jitter > 0f32 && jitter < 1f32,
"<exponential-backoff>: jitter must be between 0 and 1."
);
self.jitter = jitter;
self
}
#[inline]
pub fn factor(mut self, factor: u32) -> Self {
self.factor = factor;
self
}
#[inline]
pub fn iter(&self) -> iter::Iter {
iter::Iter::new(self)
}
}