use std::time::Duration;
use crate::constants::{
CONTENTION_ADAPTIVE_INITIAL_DELAY, CONTENTION_ADAPTIVE_JITTER_FACTOR,
CONTENTION_ADAPTIVE_MAX_ATTEMPTS, CONTENTION_ADAPTIVE_MAX_DELAY,
CONTENTION_ADAPTIVE_MAX_ELAPSED, CONTENTION_ADAPTIVE_MAX_TOTAL_ELAPSED,
LATENCY_FIRST_MAX_ATTEMPTS, LATENCY_FIRST_MAX_ELAPSED, RELIABILITY_FIRST_INITIAL_DELAY,
RELIABILITY_FIRST_JITTER_FACTOR, RELIABILITY_FIRST_MAX_ATTEMPTS, RELIABILITY_FIRST_MAX_DELAY,
RELIABILITY_FIRST_MAX_ELAPSED, RELIABILITY_FIRST_MAX_TOTAL_ELAPSED,
};
use super::CasStrategyProfile;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CasStrategy {
LatencyFirst,
ContentionAdaptive,
ReliabilityFirst,
}
impl Default for CasStrategy {
#[inline]
fn default() -> Self {
Self::LatencyFirst
}
}
impl CasStrategy {
#[inline]
pub fn profile(self) -> CasStrategyProfile {
match self {
Self::LatencyFirst => CasStrategyProfile::new(
LATENCY_FIRST_MAX_ATTEMPTS,
LATENCY_FIRST_MAX_ELAPSED,
None,
0.0,
false,
),
Self::ContentionAdaptive => CasStrategyProfile::new(
CONTENTION_ADAPTIVE_MAX_ATTEMPTS,
CONTENTION_ADAPTIVE_MAX_ELAPSED,
Some(CONTENTION_ADAPTIVE_MAX_TOTAL_ELAPSED),
0.30,
true,
),
Self::ReliabilityFirst => CasStrategyProfile::new(
RELIABILITY_FIRST_MAX_ATTEMPTS,
RELIABILITY_FIRST_MAX_ELAPSED,
Some(RELIABILITY_FIRST_MAX_TOTAL_ELAPSED),
0.50,
true,
),
}
}
#[inline]
pub(crate) fn backoff(self) -> Option<(Duration, Duration, f64)> {
match self {
Self::LatencyFirst => None,
Self::ContentionAdaptive => Some((
CONTENTION_ADAPTIVE_INITIAL_DELAY,
CONTENTION_ADAPTIVE_MAX_DELAY,
CONTENTION_ADAPTIVE_JITTER_FACTOR,
)),
Self::ReliabilityFirst => Some((
RELIABILITY_FIRST_INITIAL_DELAY,
RELIABILITY_FIRST_MAX_DELAY,
RELIABILITY_FIRST_JITTER_FACTOR,
)),
}
}
}