use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct ScalingPressureConfig {
pub enabled: bool,
pub memory_gate_threshold: f64,
}
impl Default for ScalingPressureConfig {
fn default() -> Self {
Self {
enabled: true,
memory_gate_threshold: 0.8,
}
}
}
impl ScalingPressureConfig {
#[must_use]
pub fn from_cascade() -> Self {
#[cfg(feature = "config")]
{
if let Some(cfg) = crate::config::try_get()
&& let Ok(scaling) = cfg.unmarshal_key_registered::<Self>("scaling")
{
return scaling;
}
}
Self::default()
}
}
#[derive(Debug, Clone)]
pub struct ScalingComponent {
pub name: String,
pub weight: f64,
pub saturation: f64,
}
impl ScalingComponent {
#[must_use]
pub fn new(name: impl Into<String>, weight: f64, saturation: f64) -> Self {
Self {
name: name.into(),
weight,
saturation,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_config_defaults() {
let config = ScalingPressureConfig::default();
assert!(config.enabled);
assert!((config.memory_gate_threshold - 0.8).abs() < f64::EPSILON);
}
#[test]
fn test_config_serde_roundtrip() {
let config = ScalingPressureConfig {
enabled: false,
memory_gate_threshold: 0.9,
};
let json = serde_json::to_string(&config).unwrap();
let parsed: ScalingPressureConfig = serde_json::from_str(&json).unwrap();
assert!(!parsed.enabled);
assert!((parsed.memory_gate_threshold - 0.9).abs() < f64::EPSILON);
}
#[test]
fn test_component_new() {
let c = ScalingComponent::new("kafka_lag", 0.35, 100_000.0);
assert_eq!(c.name, "kafka_lag");
assert!((c.weight - 0.35).abs() < f64::EPSILON);
assert!((c.saturation - 100_000.0).abs() < f64::EPSILON);
}
}