rtci_interceptor/gcc/
loss.rs1use std::time::{Duration, Instant};
4
5pub const DEFAULT_LOW_LOSS: f64 = 0.02;
7
8pub const DEFAULT_HIGH_LOSS: f64 = 0.10;
10
11pub const DEFAULT_LOSS_INTERVAL: Duration = Duration::from_millis(200);
13
14#[derive(Debug, Clone)]
33pub struct LossController {
34 target: f64,
36 min: f64,
37 max: f64,
38 low: f64,
39 high: f64,
40 interval: Duration,
41 average_loss: Option<f64>,
43 last_change: Option<Instant>,
44}
45
46impl LossController {
47 pub fn new(initial: f64, min: f64, max: f64) -> Self {
49 Self {
50 target: initial.clamp(min, max),
51 min,
52 max,
53 low: DEFAULT_LOW_LOSS,
54 high: DEFAULT_HIGH_LOSS,
55 interval: DEFAULT_LOSS_INTERVAL,
56 average_loss: None,
57 last_change: None,
58 }
59 }
60
61 pub fn set_bounds(&mut self, min: f64, max: f64) {
63 self.min = min;
64 self.max = max.max(min);
65 self.target = self.target.clamp(self.min, self.max);
66 }
67
68 pub fn raise(&mut self, bound: f64) {
71 self.target = self.target.max(bound).clamp(self.min, self.max);
72 }
73
74 pub fn cap(&mut self, bound: f64) {
76 self.target = self.target.min(bound).clamp(self.min, self.max);
77 }
78
79 pub fn update_fraction(&mut self, now: Instant, fraction_lost: f64) -> f64 {
82 let lost = (fraction_lost.clamp(0.0, 1.0) * 256.0).round() as usize;
84 self.update(now, lost, 256)
85 }
86
87 pub fn target(&self) -> f64 {
89 self.target
90 }
91
92 pub fn average_loss(&self) -> Option<f64> {
94 self.average_loss
95 }
96
97 pub fn update(&mut self, now: Instant, lost: usize, total: usize) -> f64 {
99 if total == 0 {
100 return self.target;
101 }
102
103 let sample = lost as f64 / total as f64;
104 let average = match self.average_loss {
107 Some(previous) => 0.8 * previous + 0.2 * sample,
108 None => sample,
109 };
110 self.average_loss = Some(average);
111
112 if let Some(last) = self.last_change
113 && now.saturating_duration_since(last) < self.interval
114 {
115 return self.target;
116 }
117
118 if average.max(sample) < self.low {
119 self.target = (self.target * 1.05).clamp(self.min, self.max);
121 self.last_change = Some(now);
122 } else if average.min(sample) > self.high {
123 self.target = (self.target * (1.0 - 0.5 * average)).clamp(self.min, self.max);
125 self.last_change = Some(now);
126 }
127 self.target
131 }
132}
133
134#[cfg(test)]
135mod tests {
136 use super::*;
137
138 const MIN: f64 = 100_000.0;
139 const MAX: f64 = 10_000_000.0;
140
141 fn controller() -> LossController {
142 LossController::new(1_000_000.0, MIN, MAX)
143 }
144
145 #[test]
148 fn heavy_loss_lowers_the_target_on_its_own() {
149 let epoch = Instant::now();
150 let mut controller = controller();
151 let before = controller.target();
152
153 let mut at = epoch;
154 for _ in 0..10 {
155 at += DEFAULT_LOSS_INTERVAL;
156 controller.update(at, 20, 100);
157 }
158
159 assert!(
160 controller.target() < before,
161 "20% loss must lower the target: {before} → {}",
162 controller.target()
163 );
164 }
165
166 #[test]
168 fn a_clean_path_raises_the_target() {
169 let epoch = Instant::now();
170 let mut controller = controller();
171 let before = controller.target();
172
173 let mut at = epoch;
174 for _ in 0..10 {
175 at += DEFAULT_LOSS_INTERVAL;
176 controller.update(at, 0, 100);
177 }
178
179 assert!(
180 controller.target() > before,
181 "a lossless path should be probed: {before} → {}",
182 controller.target()
183 );
184 }
185
186 #[test]
189 fn moderate_loss_changes_nothing() {
190 let epoch = Instant::now();
191 let mut controller = controller();
192 let before = controller.target();
193
194 let mut at = epoch;
195 for _ in 0..20 {
196 at += DEFAULT_LOSS_INTERVAL;
197 controller.update(at, 5, 100);
199 }
200
201 assert_eq!(
202 before,
203 controller.target(),
204 "loss inside the band must not move the target"
205 );
206 }
207
208 #[test]
210 fn the_backoff_is_proportional_to_the_loss() {
211 let epoch = Instant::now();
212 let mut mild = controller();
213 let mut severe = controller();
214
215 let mut at = epoch;
216 for _ in 0..5 {
217 at += DEFAULT_LOSS_INTERVAL;
218 mild.update(at, 12, 100);
219 severe.update(at, 50, 100);
220 }
221
222 assert!(
223 severe.target() < mild.target(),
224 "50% loss should back off further than 12%: {} vs {}",
225 severe.target(),
226 mild.target()
227 );
228 }
229
230 #[test]
232 fn empty_feedback_changes_nothing() {
233 let epoch = Instant::now();
234 let mut controller = controller();
235 let before = controller.target();
236
237 assert_eq!(before, controller.update(epoch, 0, 0));
238 assert_eq!(None, controller.average_loss());
239 }
240
241 #[test]
244 fn the_target_stays_within_configured_bounds() {
245 let epoch = Instant::now();
246 let mut controller = LossController::new(MIN, MIN, 400_000.0);
247
248 let mut at = epoch;
249 for _ in 0..500 {
250 at += DEFAULT_LOSS_INTERVAL;
251 controller.update(at, 0, 100);
252 }
253 assert!(
254 controller.target() <= 400_000.0,
255 "ceiling breached: {}",
256 controller.target()
257 );
258
259 for _ in 0..500 {
260 at += DEFAULT_LOSS_INTERVAL;
261 controller.update(at, 90, 100);
262 }
263 assert!(
264 controller.target() >= MIN,
265 "floor breached: {}",
266 controller.target()
267 );
268 }
269}