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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
use std::time::Duration;
use blocking_permit::Semaphore;
use body_image::Tunables;
#[derive(Debug, Clone)]
pub struct FutioTunables {
image: Tunables,
res_timeout: Option<Duration>,
body_timeout: Option<Duration>,
blocking_policy: BlockingPolicy,
stream_item_size: usize,
}
#[derive(Debug, Copy, Clone)]
pub enum BlockingPolicy {
Direct,
Permit(&'static Semaphore),
Dispatch,
}
impl FutioTunables {
pub fn new() -> FutioTunables {
FutioTunables {
image: Tunables::new(),
res_timeout: None,
body_timeout: Some(Duration::from_secs(60)),
blocking_policy: BlockingPolicy::Direct,
stream_item_size: 512 * 1024,
}
}
pub fn image(&self) -> &Tunables {
&self.image
}
pub fn stream_item_size(&self) -> usize {
self.stream_item_size
}
pub fn res_timeout(&self) -> Option<Duration> {
self.res_timeout
}
pub fn body_timeout(&self) -> Option<Duration> {
self.body_timeout
}
pub fn blocking_semaphore(&self) -> Option<&'static Semaphore> {
if let BlockingPolicy::Permit(sema) = self.blocking_policy {
Some(sema)
} else {
None
}
}
pub fn blocking_policy(&self) -> BlockingPolicy {
self.blocking_policy
}
}
impl Default for FutioTunables {
fn default() -> Self { FutioTunables::new() }
}
impl AsRef<Tunables> for FutioTunables {
fn as_ref(&self) -> &Tunables {
self.image()
}
}
pub struct FutioTuner {
template: FutioTunables
}
impl FutioTuner {
pub fn new() -> FutioTuner {
FutioTuner { template: FutioTunables::new() }
}
pub fn set_image(&mut self, image: Tunables) -> &mut FutioTuner {
self.template.image = image;
self
}
pub fn set_stream_item_size(&mut self, size: usize) -> &mut FutioTuner {
assert!(size > 0, "stream_item_size must be greater than zero");
self.template.stream_item_size = size;
self
}
pub fn set_res_timeout(&mut self, dur: Duration) -> &mut FutioTuner {
self.template.res_timeout = Some(dur);
self
}
pub fn unset_res_timeout(&mut self) -> &mut FutioTuner {
self.template.res_timeout = None;
self
}
pub fn set_body_timeout(&mut self, dur: Duration) -> &mut FutioTuner {
self.template.body_timeout = Some(dur);
self
}
pub fn unset_body_timeout(&mut self) -> &mut FutioTuner {
self.template.body_timeout = None;
self
}
pub fn set_blocking_policy(&mut self, policy: BlockingPolicy)
-> &mut FutioTuner
{
self.template.blocking_policy = policy;
self
}
pub fn finish(&self) -> FutioTunables {
let t = self.template.clone();
if t.res_timeout.is_some() && t.body_timeout.is_some() {
assert!(t.res_timeout.unwrap() <= t.body_timeout.unwrap(),
"res_timeout can't be greater than body_timeout");
}
t
}
}
impl Default for FutioTuner {
fn default() -> Self { FutioTuner::new() }
}