1use std::time::Duration;
28use std::time::Instant;
29
30const WINDOW_INCREASE_FACTOR: u64 = 2;
33
34const WINDOW_TRIGGER_FACTOR: u32 = 2;
37
38#[derive(Default, Debug)]
39pub struct FlowControl {
40 consumed: u64,
42
43 max_data: u64,
45
46 window: u64,
49
50 max_window: u64,
52
53 last_update: Option<Instant>,
55}
56
57impl FlowControl {
58 pub fn new(max_data: u64, window: u64, max_window: u64) -> Self {
59 Self {
60 max_data,
61
62 window,
63
64 max_window,
65
66 ..Default::default()
67 }
68 }
69
70 pub fn window(&self) -> u64 {
72 self.window
73 }
74
75 pub fn max_data(&self) -> u64 {
77 self.max_data
78 }
79
80 #[cfg(test)]
82 pub fn consumed(&self) -> u64 {
83 self.consumed
84 }
85
86 pub fn add_consumed(&mut self, consumed: u64) {
88 self.consumed += consumed;
89 }
90
91 pub fn should_update_max_data(&self) -> bool {
96 let available_window = self.max_data - self.consumed;
97
98 available_window < (self.window / 2)
99 }
100
101 pub fn max_data_next(&self) -> u64 {
103 self.consumed + self.window
104 }
105
106 pub fn update_max_data(&mut self, now: Instant) {
108 self.max_data = self.max_data_next();
109 self.last_update = Some(now);
110 }
111
112 pub fn autotune_window(&mut self, now: Instant, rtt: Duration) {
116 if let Some(last_update) = self.last_update {
117 if now - last_update < rtt * WINDOW_TRIGGER_FACTOR {
118 self.window = std::cmp::min(
119 self.window * WINDOW_INCREASE_FACTOR,
120 self.max_window,
121 );
122 }
123 }
124 }
125
126 pub fn ensure_window_lower_bound(&mut self, min_window: u64) {
129 if min_window > self.window {
130 self.window = std::cmp::min(min_window, self.max_window);
132 }
133 }
134}
135
136#[cfg(test)]
137mod tests {
138 use super::*;
139
140 #[test]
141 fn max_data() {
142 let fc = FlowControl::new(100, 20, 100);
143
144 assert_eq!(fc.max_data(), 100);
145 }
146
147 #[test]
148 fn should_update_max_data() {
149 let mut fc = FlowControl::new(100, 20, 100);
150
151 fc.add_consumed(85);
152 assert!(!fc.should_update_max_data());
153
154 fc.add_consumed(10);
155 assert!(fc.should_update_max_data());
156 }
157
158 #[test]
159 fn max_data_next() {
160 let mut fc = FlowControl::new(100, 20, 100);
161
162 let consumed = 95;
163
164 fc.add_consumed(consumed);
165 assert!(fc.should_update_max_data());
166 assert_eq!(fc.max_data_next(), consumed + 20);
167 }
168
169 #[test]
170 fn update_max_data() {
171 let mut fc = FlowControl::new(100, 20, 100);
172
173 let consumed = 95;
174
175 fc.add_consumed(consumed);
176 assert!(fc.should_update_max_data());
177
178 let max_data_next = fc.max_data_next();
179 assert_eq!(fc.max_data_next(), consumed + 20);
180
181 fc.update_max_data(Instant::now());
182 assert_eq!(fc.max_data(), max_data_next);
183 }
184
185 #[test]
186 fn autotune_window() {
187 let w = 20;
188 let mut fc = FlowControl::new(100, w, 100);
189
190 let consumed = 95;
191
192 fc.add_consumed(consumed);
193 assert!(fc.should_update_max_data());
194
195 let max_data_next = fc.max_data_next();
196 assert_eq!(max_data_next, consumed + w);
197
198 fc.update_max_data(Instant::now());
199 assert_eq!(fc.max_data(), max_data_next);
200
201 fc.autotune_window(Instant::now(), Duration::from_millis(100));
203
204 let w = w * 2;
205 let consumed_inc = 15;
206
207 fc.add_consumed(consumed_inc);
208 assert!(fc.should_update_max_data());
209
210 let max_data_next = fc.max_data_next();
211 assert_eq!(max_data_next, consumed + consumed_inc + w);
212 }
213
214 #[test]
215 fn ensure_window_lower_bound() {
216 let w = 20;
217 let mut fc = FlowControl::new(100, w, 100);
218
219 fc.ensure_window_lower_bound(w);
221 assert_eq!(fc.window(), 20);
222
223 fc.ensure_window_lower_bound(w * 2);
225 assert_eq!(fc.window(), 40);
226 }
227}