qorb 0.4.1

Connection Pooling
Documentation
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! An implementation of the Controlled Delay algorithm.
//!
//! Refer to https://queue.acm.org/appendices/codel.html for
//! additional context.

use std::time::{Duration, Instant};

/// Parameters to customize the Controlled Delay algorithm.
///
/// Refer to the documentation on [ControlledDelay] for
/// additional context.
#[derive(Clone, Debug)]
pub struct ControlledDelayParameters {
    /// The duration of travel through a healthy queue.
    pub target: Duration,

    /// A window in which a healthy "target" duration should be reached.
    pub interval: Duration,

    /// A multiplier of the interval beyond which we must "miss" the target
    /// to initially to start dropping from the queue.
    ///
    /// A value of "N" means we'll drop connections as soon as we've passed
    /// (1 + N) intervals without beating the target duration.
    pub interval_hysteresis_factor: u32,
}

impl Default for ControlledDelayParameters {
    fn default() -> Self {
        Self {
            target: Duration::from_millis(5),
            interval: Duration::from_millis(100),
            interval_hysteresis_factor: 1,
        }
    }
}

/// Controlled Delay (or "CoDel") is an algorithm to manage queues.
///
/// This algorithm monitors latency through a queue, and gives feedback
/// about whether or not the latency through that queue is bufferbloat
/// (undue end-to-end latency due to stalling in a queue).
///
/// This algorithm works with the following parameters:
/// - Interval: A window of time in which we are monitoring the queue.
/// - Target Delay: The "maximum acceptable queue time" that we expect
/// a packet to take when moving through the queue.
///
/// While observing a queue for an Interval duration:
/// - If we observe any packets moving through the queue faster than our "target
/// delay", the queue is working as expected!
/// - If we do not observe any packets beating the "target time" - in other
/// words, servicing requests in the queue is taking too long - then the queue
/// is overloaded, and requests should be dropped.
#[derive(Debug)]
pub struct ControlledDelay {
    params: ControlledDelayParameters,
    must_hit_target_by: Option<Instant>,

    last_emptied: Option<Instant>,

    // The time to drop the next packet.
    drop_next: Option<Instant>,
    // Packets dropped since entering drop state.
    drop_count: usize,
    // True if we're dropping packets form the queue.
    dropping: bool,
}

impl ControlledDelay {
    pub fn new(params: ControlledDelayParameters) -> Self {
        Self {
            params,
            must_hit_target_by: None,

            last_emptied: None,

            drop_next: None,
            drop_count: 0,
            dropping: false,
        }
    }

    fn can_drop(&mut self, now: Instant, start: Instant) -> bool {
        let sojourn_time = now - start;

        // If the queue is moving quickly enough, we shouldn't drop anything.
        //
        // This also clears the tracking that we need to hit the target.
        if sojourn_time < self.params.target {
            self.must_hit_target_by = None;
            return false;
        }

        match self.must_hit_target_by {
            Some(must_hit_target_by) => {
                // Return if we should have already hit the target, but haven't.
                return must_hit_target_by <= now;
            }
            None => {
                // If we haven't started an interval yet, do so now.
                self.must_hit_target_by = Some(now + self.params.interval);
                return false;
            }
        }
    }

    /// Given the starting time of a claim, returns if the request
    /// should time out or not.
    pub fn should_drop(&mut self, start: Instant) -> bool {
        self.should_drop_inner(Instant::now(), start)
    }

    // This is a pattern used throughout this module:
    //
    // Any functions depending on "now" take it as an input parameter,
    // so we can more easily create deterministic tests.
    fn should_drop_inner(&mut self, now: Instant, start: Instant) -> bool {
        let ok_to_drop = self.can_drop(now, start);

        if self.dropping {
            if !ok_to_drop {
                self.dropping = false;
                return false;
            }

            if let Some(drop_next) = self.drop_next {
                if now >= drop_next {
                    self.drop_count += 1;
                    self.set_drop_next(now);
                    return true;
                }
            }
            return false;
        }

        let failed_target_for_a_while = self
            .must_hit_target_by
            .map(|must_hit_target_by| {
                // Note that when "must_hit_target_by" is set, it gets
                // set to "now + interval". Admittedly, that's a different
                // value of "now" than the one being evaluated here.
                now - must_hit_target_by
                    >= self.params.interval * self.params.interval_hysteresis_factor
            })
            .unwrap_or(false);

        let dropped_recently = self
            .drop_next
            .map(|drop_next| now - drop_next < self.params.interval)
            .unwrap_or(false);

        if ok_to_drop && (failed_target_for_a_while || dropped_recently) {
            self.dropping = true;

            if dropped_recently {
                self.drop_count = if self.drop_count > 2 {
                    self.drop_count - 2
                } else {
                    1
                };
            } else {
                self.drop_count = 1;
            }

            self.set_drop_next(now);

            return true;
        }

        return false;
    }

    fn set_drop_next(&mut self, now: Instant) {
        if self.drop_count > 0 {
            self.drop_next = Some(
                now + (self
                    .params
                    .interval
                    .div_f64((self.drop_count as f64).sqrt())),
            );
        }
    }

    pub fn queue_cleared(&mut self) {
        self.queue_cleared_inner(Instant::now());
    }

    fn queue_cleared_inner(&mut self, now: Instant) {
        self.last_emptied = Some(now);
        self.drop_next = None;
        self.dropping = false;
        self.drop_count = 0;
        self.must_hit_target_by = None;
    }

    /// Get the maximum duration for a request to be idle in the queue
    /// before timing out.
    pub fn get_max_idle(&self) -> Duration {
        self.get_max_idle_inner(Instant::now())
    }

    fn get_max_idle_inner(&self, now: Instant) -> Duration {
        let bound = self.params.target * 10;
        if let Some(last_emptied) = self.last_emptied {
            if last_emptied < now - bound {
                return self.params.target * 3;
            }
        }
        return bound;
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use std::collections::VecDeque;

    fn ms(ms: u64) -> Duration {
        Duration::from_millis(ms)
    }

    struct TestHarness {
        cd: ControlledDelay,
        test_start: Instant,
        now: Instant,
        count: usize,
        entries: VecDeque<(usize, Instant)>,
    }

    impl TestHarness {
        fn new(cd: ControlledDelay) -> Self {
            let now = Instant::now();
            Self {
                cd,
                test_start: now,
                now,
                count: 0,
                entries: VecDeque::new(),
            }
        }

        // Add an entry to the queue at "now"
        fn push(&mut self) -> &mut Self {
            self.entries.push_back((self.count, self.now));
            self.count += 1;
            self
        }

        // Wait for a certain length of time
        fn wait(&mut self, length: Duration) -> &mut Self {
            self.now = self.now.checked_add(length).unwrap();
            self
        }

        // Pop an entry from the queue, and assert that we should
        // not drop it.
        fn expect_dequeue(&mut self) -> &mut Self {
            let (count, start) = self.entries.pop_front().unwrap();
            let now = self.now;

            assert!(
                !self.cd.should_drop_inner(now, start),
                "Expected that we would not drop this entry:\n\
                 \tEntry #{count} (zero-indexed) \n\
                 \tEntered Queue at {entry} ms \n\
                 \tExited Queue at {exit} ms \n\
                 {cd}\n",
                entry = (start - self.test_start).as_millis(),
                exit = (now - self.test_start).as_millis(),
                cd = self.debug_cd(),
            );
            self
        }

        // Pop an entry from the queue, and assert that we should
        // drop it.
        fn expect_drop(&mut self) -> &mut Self {
            let (count, start) = self.entries.pop_front().unwrap();
            let now = self.now;

            assert!(
                self.cd.should_drop_inner(now, start),
                "Expected that we would drop this entry:\n\
                 \tEntry #{count} (zero-indexed) \n\
                 \tEntered Queue at {entry} ms \n\
                 \tExited Queue at {exit} ms\n\
                 {cd}\n",
                entry = (start - self.test_start).as_millis(),
                exit = (now - self.test_start).as_millis(),
                cd = self.debug_cd(),
            );
            self
        }

        fn debug_cd(&self) -> String {
            let must_hit_target_by = match self.cd.must_hit_target_by {
                Some(must_hit_target_by) => {
                    format!(
                        "{} ms",
                        must_hit_target_by
                            .duration_since(self.test_start)
                            .as_millis()
                    )
                }
                None => "None".to_string(),
            };

            let drop_next = match self.cd.drop_next {
                Some(drop_next) => {
                    format!(
                        "{} ms",
                        drop_next.duration_since(self.test_start).as_millis()
                    )
                }
                None => "None".to_string(),
            };

            format!(
                "ControlledDelay:\n\
                \tdropping: {dropping}\n\
                \tmust_hit_target_by: {must_hit_target_by},\n\
                \tdrop_next: {drop_next}\n\
                \tdrop_count: {drop_count}",
                dropping = self.cd.dropping,
                drop_count = self.cd.drop_count,
            )
        }
    }

    #[test]
    fn quick_requests_do_not_overload() {
        let params = ControlledDelayParameters {
            target: ms(5),
            interval: ms(100),
            interval_hysteresis_factor: 0,
        };
        let cd = ControlledDelay::new(params.clone());
        let mut harness = TestHarness::new(cd);

        harness
            .push() // >-> 1 ms to complete
            .wait(ms(1)) //   |
            .push() // >-|-> completes immediately
            .expect_dequeue() // <-< |
            .expect_dequeue(); // <---<
    }

    #[test]
    fn slow_requests_cause_drops() {
        let params = ControlledDelayParameters {
            target: ms(5),
            interval: ms(100),
            interval_hysteresis_factor: 0,
        };
        let cd = ControlledDelay::new(params.clone());
        let mut harness = TestHarness::new(cd);

        harness
            .push() // >-> 100 ms to complete
            .push() // >-|-> 200 ms to complete
            .push() // >-|-|-> 300 ms to complete
            .wait(ms(100)) //   | | |
            .expect_dequeue() // <-< | |
            .wait(ms(100)) //     | |
            .expect_drop() // <---< |
            .wait(ms(100)) //       |
            .expect_drop(); // <-----<
    }

    #[test]
    fn keep_dropping_at_interval() {
        let params = ControlledDelayParameters {
            target: ms(5),
            interval: ms(100),
            interval_hysteresis_factor: 0,
        };
        let cd = ControlledDelay::new(params.clone());
        let mut harness = TestHarness::new(cd);

        // Request #0
        harness
            .push()
            .wait(ms(100))
            // Sets deadline to "100 ms from now"
            .expect_dequeue();

        // Request #1
        harness
            .push()
            .wait(ms(100))
            // Barely Misses deadline, gets dropped. Also sets
            // "drop_next" to:
            //   now + interval / sqrt(drop_count)
            // = now + 100
            .expect_drop();

        // Request #2
        harness
            .push()
            .wait(ms(100))
            // Sets "drop_next" to:
            //   now + interval / sqrt(drop_count)
            // = now + 70.7
            .expect_drop();

        // Request #3
        harness
            .push()
            .wait(ms(71))
            // Sets "drop_next" to:
            //   now + interval / sqrt(drop_count)
            // = now + 57.7
            .expect_drop();

        // Request #4
        harness.push().wait(ms(58)).expect_drop();
    }

    #[test]
    fn recover_from_dropping_then_hit_drop_next_timeout() {
        let params = ControlledDelayParameters {
            target: ms(5),
            interval: ms(100),
            interval_hysteresis_factor: 0,
        };
        let cd = ControlledDelay::new(params.clone());
        let mut harness = TestHarness::new(cd);

        harness
            .push() // #0
            .wait(ms(100))
            .expect_dequeue()
            .push() // #1
            .wait(ms(100))
            .expect_drop()
            .push() // #2
            .wait(ms(100))
            .expect_drop()
            .push() // #3
            // See "keep_dropping_at_interval" above, if this was 71 ms,
            // it would be dropped.
            .wait(ms(70))
            .expect_dequeue()
            .push() // #4
            // As long as we're slower than the "target time",
            // we'll pass the "drop_next" interval on this request.
            .wait(ms(6))
            .expect_drop();
    }
}