1
Fork 0
mirror of https://github.com/RGBCube/superfreq synced 2025-07-27 17:07:44 +00:00

daemon: enforce >= 1s minimum interval in clamping

This commit is contained in:
NotAShelf 2025-05-18 08:11:46 +03:00
parent 8c462868b6
commit 34862b28e1
No known key found for this signature in database
GPG key ID: 29D95B64378DB4BF

View file

@ -118,22 +118,23 @@ fn compute_new(params: &IntervalParams, system_history: &SystemHistory) -> u64 {
} }
// Ensure interval stays within configured bounds // Ensure interval stays within configured bounds
let new_interval = adjusted_interval.clamp(params.min_interval, params.max_interval); // Enforce a minimum of 1 second to prevent busy loops, regardless of params.min_interval
let min_safe_interval = params.min_interval.max(1);
let new_interval = adjusted_interval.clamp(min_safe_interval, params.max_interval);
// Blend the new interval with the cached value if available // Blend the new interval with the cached value if available
let blended_interval = if let Some(cached) = system_history.last_computed_interval { let blended_interval = if let Some(cached) = system_history.last_computed_interval {
// Use a weighted average: 70% previous value, 30% new value // Use a weighted average: 70% previous value, 30% new value
// This smooths out drastic changes in polling frequency // This smooths out drastic changes in polling frequency
// We use integer arithmetic to avoid precision loss with large interval values // Use integer arithmetic to avoid precision loss with large interval values
// I think Clippy warned me about this at some point and I ignored it. Now I come
// crawling back to it...
(cached * 7 + new_interval * 3) / 10 (cached * 7 + new_interval * 3) / 10
} else { } else {
new_interval new_interval
}; };
// Blended result still needs to respect the configured bounds // Blended result still needs to respect the configured bounds
blended_interval.clamp(params.min_interval, params.max_interval) // Again enforce minimum of 1 second regardless of params.min_interval
blended_interval.clamp(min_safe_interval, params.max_interval)
} }
/// Tracks historical system data for "advanced" adaptive polling /// Tracks historical system data for "advanced" adaptive polling