feat: Improve concurrency
This commit is contained in:
+25
-14
@@ -4,6 +4,7 @@ use std::str::FromStr;
|
||||
|
||||
use async_walkdir::WalkDir;
|
||||
use futures::StreamExt;
|
||||
use futures::future::join_all;
|
||||
use futures::stream::FuturesUnordered;
|
||||
use minijinja::Environment;
|
||||
use optional_struct::{Applicable, optional_struct};
|
||||
@@ -87,7 +88,11 @@ impl Cluster {
|
||||
|
||||
let default: OptionalClusterDeserialize = if path.exists() {
|
||||
let content = tokio::fs::read_to_string(path).await.unwrap();
|
||||
serde_yaml::from_str(&content).unwrap()
|
||||
|
||||
// The deserialization makes a blocking web request
|
||||
tokio::task::spawn_blocking(move || serde_yaml::from_str(&content).unwrap())
|
||||
.await
|
||||
.unwrap()
|
||||
} else {
|
||||
Default::default()
|
||||
};
|
||||
@@ -134,29 +139,35 @@ impl Cluster {
|
||||
pub async fn get_all() -> Vec<Cluster> {
|
||||
let env = PathEnvironment::new_patches();
|
||||
|
||||
WalkDir::new(get_talos_path().join("clusters"))
|
||||
let names: Vec<_> = WalkDir::new(get_talos_path().join("clusters"))
|
||||
.filter_map(|e| async { e.ok() })
|
||||
.filter(|e| {
|
||||
let e = e.clone();
|
||||
async move { e.metadata().await.unwrap().is_file() }
|
||||
})
|
||||
.filter_map(|e| {
|
||||
let env = env.clone();
|
||||
async move {
|
||||
let mut path = PathBuf::from_str(&e.file_name().to_string_lossy()).unwrap();
|
||||
path.set_extension("");
|
||||
.filter_map(|e| async move {
|
||||
let mut path = PathBuf::from_str(&e.file_name().to_string_lossy()).unwrap();
|
||||
path.set_extension("");
|
||||
|
||||
let name = path.to_string_lossy().to_string();
|
||||
let name = path.to_string_lossy().to_string();
|
||||
|
||||
if name == "default" {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(Cluster::get(&name, &env).await)
|
||||
if name == "default" {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(name)
|
||||
})
|
||||
.collect()
|
||||
.await
|
||||
.await;
|
||||
|
||||
// Get the clusters concurrently
|
||||
join_all(names.iter().map(|name| {
|
||||
let env = env.clone();
|
||||
let name = name.clone();
|
||||
|
||||
async move { Cluster::get(&name, &env).await }
|
||||
}))
|
||||
.await
|
||||
}
|
||||
|
||||
pub fn nodes(&self) -> &[Node] {
|
||||
|
||||
Reference in New Issue
Block a user