Clamp JSON CPU count to available CPU count

This commit is contained in:
NGnius (Graham) 2022-09-05 21:56:15 -04:00
parent 7f60a00ee4
commit 86471990bb
2 changed files with 16 additions and 1 deletions

2
backend/Cargo.lock generated
View file

@ -567,7 +567,7 @@ dependencies = [
[[package]]
name = "powertools-rs"
version = "1.0.0-rc2"
version = "1.0.0"
dependencies = [
"log",
"serde",

View file

@ -101,9 +101,24 @@ impl Settings {
fn convert_cpus(mut cpus: Vec<CpuJson>, version: u64) -> Vec<Cpu> {
let mut result = Vec::with_capacity(cpus.len());
let max_cpus = Cpu::cpu_count();
for (i, cpu) in cpus.drain(..).enumerate() {
// prevent having more CPUs than available
if let Some(max_cpus) = max_cpus {
if i == max_cpus {
break;
}
}
result.push(Cpu::from_json(cpu, version, i));
}
if let Some(max_cpus) = max_cpus {
if result.len() != max_cpus {
let mut sys_cpus = Cpu::system_default();
for i in result.len()..sys_cpus.len() {
result.push(sys_cpus.remove(i));
}
}
}
result
}