Skip to main content

adaptive_admission_core/
error.rs

1use std::error::Error;
2use std::fmt::{self, Display, Formatter};
3
4use crate::{RequestId, Timestamp};
5
6/// Invalid controller configuration.
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8pub enum ConfigError {
9    ThresholdOutOfRange { value: u16 },
10    ZeroConcurrency,
11    ZeroQueueTimeout,
12    QueueTimeoutRatioOutOfRange,
13    ZeroStatisticsMinWindow,
14    StatisticsWindowOrder { minimum_ms: u64, maximum_ms: u64 },
15    ZeroStatisticsMinSamples,
16    ZeroStatisticsPercentileSamples,
17    StatisticsSampleLimitTooSmall { maximum: usize, required: usize },
18    StatisticsPercentileOutOfRange { value: u8 },
19    InvalidPidGain { name: &'static str },
20    ZeroPidInterval,
21    PidDeltaOutOfRange,
22    PidDecayOutOfRange,
23}
24
25impl Display for ConfigError {
26    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
27        match self {
28            Self::ThresholdOutOfRange { value } => {
29                write!(formatter, "admission threshold {value} is outside 0..=768")
30            }
31            Self::ZeroConcurrency => write!(formatter, "max concurrency must be greater than zero"),
32            Self::ZeroQueueTimeout => write!(formatter, "queue timeout must be greater than zero"),
33            Self::QueueTimeoutRatioOutOfRange => {
34                write!(
35                    formatter,
36                    "queue timeout ratio must be finite and within 0 < ratio <= 1"
37                )
38            }
39            Self::ZeroStatisticsMinWindow => {
40                write!(
41                    formatter,
42                    "statistics minimum window must be greater than zero"
43                )
44            }
45            Self::StatisticsWindowOrder {
46                minimum_ms,
47                maximum_ms,
48            } => write!(
49                formatter,
50                "statistics maximum window {maximum_ms} ms is shorter than minimum window {minimum_ms} ms"
51            ),
52            Self::ZeroStatisticsMinSamples => {
53                write!(
54                    formatter,
55                    "statistics minimum samples must be greater than zero"
56                )
57            }
58            Self::ZeroStatisticsPercentileSamples => write!(
59                formatter,
60                "statistics minimum percentile samples must be greater than zero"
61            ),
62            Self::StatisticsSampleLimitTooSmall { maximum, required } => write!(
63                formatter,
64                "statistics sample limit {maximum} is smaller than required sample count {required}"
65            ),
66            Self::StatisticsPercentileOutOfRange { value } => {
67                write!(
68                    formatter,
69                    "statistics percentile {value} is outside 0..=100"
70                )
71            }
72            Self::InvalidPidGain { name } => {
73                write!(formatter, "PID {name} gain must be finite and non-negative")
74            }
75            Self::ZeroPidInterval => {
76                write!(formatter, "PID interval must be greater than zero")
77            }
78            Self::PidDeltaOutOfRange => {
79                write!(
80                    formatter,
81                    "PID maximum delta must be finite and within 0..=100"
82                )
83            }
84            Self::PidDecayOutOfRange => {
85                write!(
86                    formatter,
87                    "PID integral decay must be finite and within 0..=1"
88                )
89            }
90        }
91    }
92}
93
94impl Error for ConfigError {}
95
96/// Invalid priority construction.
97#[derive(Clone, Copy, Debug, Eq, PartialEq)]
98pub enum PriorityError {
99    ValueOutOfRange { value: u16 },
100    TierOutOfRange { tier: u8 },
101    CohortOutOfRange { cohort: u8 },
102}
103
104impl Display for PriorityError {
105    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
106        match self {
107            Self::ValueOutOfRange { value } => {
108                write!(formatter, "priority {value} is outside 0..=767")
109            }
110            Self::TierOutOfRange { tier } => {
111                write!(formatter, "priority tier {tier} is outside 0..=5")
112            }
113            Self::CohortOutOfRange { cohort } => {
114                write!(formatter, "priority cohort {cohort} is outside 0..=127")
115            }
116        }
117    }
118}
119
120impl Error for PriorityError {}
121
122/// Failure to apply an event to the current controller state.
123#[derive(Clone, Copy, Debug, Eq, PartialEq)]
124pub enum ControllerError {
125    TimeRegression {
126        previous: Timestamp,
127        received: Timestamp,
128    },
129    UnknownRequest {
130        request_id: RequestId,
131    },
132    InvalidTransition {
133        request_id: RequestId,
134        operation: &'static str,
135        state: &'static str,
136    },
137    RequestIdExhausted,
138    SequenceExhausted,
139    DeadlineOverflow,
140}
141
142impl Display for ControllerError {
143    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
144        match self {
145            Self::TimeRegression { previous, received } => write!(
146                formatter,
147                "timestamp {} is earlier than the previous timestamp {}",
148                received.as_millis(),
149                previous.as_millis()
150            ),
151            Self::UnknownRequest { request_id } => {
152                write!(
153                    formatter,
154                    "request {} is not active or queued",
155                    request_id.value()
156                )
157            }
158            Self::InvalidTransition {
159                request_id,
160                operation,
161                state,
162            } => write!(
163                formatter,
164                "cannot {operation} request {} while it is {state}",
165                request_id.value()
166            ),
167            Self::RequestIdExhausted => write!(formatter, "request ID space is exhausted"),
168            Self::SequenceExhausted => write!(formatter, "queue sequence space is exhausted"),
169            Self::DeadlineOverflow => {
170                write!(formatter, "queue deadline exceeds the timestamp range")
171            }
172        }
173    }
174}
175
176impl Error for ControllerError {}