#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AdmissionDecision {
Admit,
WouldThrottle,
Throttle {
retry_after_secs: u32,
},
Oversized,
}
impl AdmissionDecision {
#[must_use]
pub const fn is_admitted(self) -> bool {
matches!(self, Self::Admit | Self::WouldThrottle)
}
#[must_use]
pub const fn retry_after_secs(self) -> Option<u32> {
match self {
Self::Throttle { retry_after_secs } => Some(retry_after_secs),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_rejections_stop_request_processing() {
assert!(AdmissionDecision::Admit.is_admitted());
assert!(AdmissionDecision::WouldThrottle.is_admitted());
assert!(
!AdmissionDecision::Throttle {
retry_after_secs: 3
}
.is_admitted()
);
assert!(!AdmissionDecision::Oversized.is_admitted());
}
#[test]
fn retry_hint_exists_only_for_retryable_rejection() {
assert_eq!(
AdmissionDecision::Throttle {
retry_after_secs: 7
}
.retry_after_secs(),
Some(7)
);
assert_eq!(AdmissionDecision::Oversized.retry_after_secs(), None);
assert_eq!(AdmissionDecision::Admit.retry_after_secs(), None);
assert_eq!(AdmissionDecision::WouldThrottle.retry_after_secs(), None);
}
}