use super::ThrottleStateView;
pub type ThrottlingStrategy = fn(state: &ThrottleStateView) -> f64;
pub fn power_curve_strategy(state: &ThrottleStateView) -> f64 {
let curve = state.config.curve_factor;
let deviation = (state.current_balance as f64 - state.learned_balance).abs();
let severity = (deviation - state.config.healthy_balance_width as f64).max(0.0);
let max_sev = (state.config.max_imbalance - state.config.healthy_balance_width) as f64;
if max_sev <= 0.0 {
return if severity > 0.0 { 1.0 } else { 0.0 };
}
let x = (severity / max_sev).clamp(0.0, 1.0);
x.powf(curve)
}
pub fn linear_strategy(state: &ThrottleStateView) -> f64 {
let deviation = (state.current_balance as f64 - state.learned_balance).abs();
let severity = (deviation - state.config.healthy_balance_width as f64).max(0.0);
let max_possible_severity =
(state.config.max_imbalance - state.config.healthy_balance_width) as f64;
if max_possible_severity <= 0.0 {
return if severity > 0.0 { 1.0 } else { 0.0 };
}
(severity / max_possible_severity).clamp(0.0, 1.0)
}