Skip to main content

rtci_interceptor/gcc/
state.rs

1//! The AIMD state machine: what a usage signal means for the rate.
2
3use super::overuse::Usage;
4
5/// What the rate controller should be doing.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
7pub enum RateControlState {
8    /// Hold the current rate — used immediately after a decrease, so the effect of the backoff can
9    /// be observed before changing anything again.
10    Hold,
11    /// Climb, looking for more capacity. The state the controller starts in
12    /// (draft-ietf-rmcat-gcc-02 section 5.5).
13    #[default]
14    Increase,
15    /// Back off.
16    Decrease,
17}
18
19impl RateControlState {
20    /// The next state, given what the delay signal now says.
21    ///
22    /// Pure: no clock, no rate, no history beyond the current state. That is what makes it
23    /// exhaustively testable — the whole machine is nine transitions.
24    ///
25    /// The asymmetry is deliberate and is the heart of AIMD: **overuse always goes straight to
26    /// `Decrease`**, from any state, because congestion is urgent; but recovery goes through
27    /// `Hold` first, so the rate does not resume climbing before the backoff has taken effect.
28    pub fn next(self, usage: Usage) -> Self {
29        match (self, usage) {
30            // Congestion: back off at once, wherever we were.
31            (_, Usage::Over) => Self::Decrease,
32
33            // The queue is draining. Hold: the path is still recovering from whatever emptied it,
34            // and climbing now is what makes an estimate oscillate.
35            (_, Usage::Under) => Self::Hold,
36
37            // Quiet. Climb, unless we have just decreased — then hold once, to see the result.
38            (Self::Hold, Usage::Normal) => Self::Increase,
39            (Self::Increase, Usage::Normal) => Self::Increase,
40            (Self::Decrease, Usage::Normal) => Self::Hold,
41        }
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    /// Overuse is urgent: it goes to `Decrease` from anywhere, with no intermediate state.
50    /// draft-ietf-rmcat-gcc-02 section 5.5: "The subsystem starts in the increase state".
51    #[test]
52    fn the_controller_starts_in_the_increase_state() {
53        assert_eq!(RateControlState::Increase, RateControlState::default());
54    }
55
56    #[test]
57    fn overuse_always_decreases() {
58        for state in [
59            RateControlState::Hold,
60            RateControlState::Increase,
61            RateControlState::Decrease,
62        ] {
63            assert_eq!(
64                RateControlState::Decrease,
65                state.next(Usage::Over),
66                "from {state:?}"
67            );
68        }
69    }
70
71    /// Recovery is not: after a decrease, one hold before climbing again. Without it the rate
72    /// resumes climbing before the backoff has reached the far end, and oscillates.
73    #[test]
74    fn recovery_holds_once_before_climbing() {
75        let after_backoff = RateControlState::Decrease.next(Usage::Normal);
76        assert_eq!(RateControlState::Hold, after_backoff);
77
78        assert_eq!(
79            RateControlState::Increase,
80            after_backoff.next(Usage::Normal),
81            "and then it may climb"
82        );
83    }
84
85    /// A quiet path keeps climbing, which is what finds capacity that has appeared.
86    #[test]
87    fn a_quiet_path_keeps_increasing() {
88        let mut state = RateControlState::Increase;
89        for _ in 0..10 {
90            state = state.next(Usage::Normal);
91        }
92        assert_eq!(RateControlState::Increase, state);
93    }
94
95    /// A draining queue holds rather than climbing: something else is using the path, or the
96    /// backlog is still clearing.
97    #[test]
98    fn a_draining_queue_holds() {
99        for state in [
100            RateControlState::Hold,
101            RateControlState::Increase,
102            RateControlState::Decrease,
103        ] {
104            assert_eq!(
105                RateControlState::Hold,
106                state.next(Usage::Under),
107                "from {state:?}"
108            );
109        }
110    }
111
112    /// The machine is total: nine transitions, all defined, none panicking.
113    #[test]
114    fn every_transition_is_defined() {
115        for state in [
116            RateControlState::Hold,
117            RateControlState::Increase,
118            RateControlState::Decrease,
119        ] {
120            for usage in [Usage::Normal, Usage::Over, Usage::Under] {
121                let _ = state.next(usage);
122            }
123        }
124    }
125}