subx_cli/core/parallel/
load_balancer.rs

1//! Load balancer for dynamic adjustment of worker distributions
2use std::collections::VecDeque;
3use std::time::{Duration, Instant};
4
5/// Balancer that tracks CPU and I/O load history and suggests optimal worker distribution.
6#[derive(Debug, Clone)]
7pub struct LoadBalancer {
8    cpu_load_history: VecDeque<f64>,
9    io_load_history: VecDeque<f64>,
10    last_balance_time: Instant,
11    balance_interval: Duration,
12}
13
14impl LoadBalancer {
15    /// Create a new load balancer with default settings.
16    pub fn new() -> Self {
17        Self {
18            cpu_load_history: VecDeque::with_capacity(10),
19            io_load_history: VecDeque::with_capacity(10),
20            last_balance_time: Instant::now(),
21            balance_interval: Duration::from_secs(30),
22        }
23    }
24
25    /// Check if it's time to rebalance according to the interval.
26    pub fn should_rebalance(&self) -> bool {
27        self.last_balance_time.elapsed() >= self.balance_interval
28    }
29
30    /// Calculate optimal distribution of CPU and I/O workers.
31    ///
32    /// Returns a tuple (cpu_workers, io_workers) indicating suggested new limits.
33    pub fn calculate_optimal_distribution(&self) -> (usize, usize) {
34        // Default behavior: maintain current distribution
35        // Actual balancing logic to be implemented in future extensions.
36        (0, 0)
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn test_load_balancer_defaults() {
46        let balancer = LoadBalancer::new();
47        assert!(!balancer.should_rebalance());
48        let (cpu, io) = balancer.calculate_optimal_distribution();
49        assert_eq!((cpu, io), (0, 0));
50    }
51}