Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/aqua/aqua_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,15 @@ use indexmap::IndexSet;
use itertools::Itertools;
use regex::Regex;
use serde_derive::Deserialize;
use std::cmp::PartialEq;
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::LazyLock as Lazy;
use std::sync::LazyLock;
use std::{
cmp::PartialEq,
sync::atomic::{AtomicBool, Ordering},
};
use tokio::sync::Mutex;
use url::Url;

#[allow(clippy::invisible_characters)]
Expand Down Expand Up @@ -197,18 +202,36 @@ impl AquaRegistry {
}

pub async fn package(&self, id: &str) -> Result<AquaPackage> {
static CACHE: LazyLock<Mutex<HashMap<String, AquaPackage>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));
static RATE_LIMITED: AtomicBool = AtomicBool::new(false);
if let Some(pkg) = CACHE.lock().await.get(id) {
return Ok(pkg.clone());
}
let path_id = id.split('/').join(std::path::MAIN_SEPARATOR_STR);
let path = self.path.join("pkgs").join(&path_id).join("registry.yaml");
let registry: RegistryYaml = if !self.repo_exists {
if let Some(registry) = AQUA_STANDARD_REGISTRY_FILES.get(id) {
trace!("reading baked-in aqua-registry for {id}");
serde_yaml::from_str(registry)?
} else if !path.exists() || file::modified_duration(&path)? > DAILY {
if RATE_LIMITED.load(Ordering::Relaxed) {
warn!("aqua-registry rate limited, skipping {id}");
return Err(eyre!("aqua-registry rate limited"));
}
trace!("downloading aqua-registry for {id} to {path:?}");
let url: Url =
format!("https://mise-versions.jdx.dev/aqua-registry/{path_id}/registry.yaml")
.parse()?;
http::HTTP_FETCH.download_file(url, &path, None).await?;
match http::HTTP_FETCH.download_file(url, &path, None).await {
Ok(_) => {}
Err(e) if http::error_code(&e) == Some(429) => {
warn!("aqua-registry rate limited, skipping {id}");
RATE_LIMITED.store(true, Ordering::Relaxed);
return Err(e);
}
Err(e) => return Err(e),
}
serde_yaml::from_reader(file::open(&path)?)?
} else {
trace!("reading cached aqua-registry for {id} from {path:?}");
Expand All @@ -226,6 +249,7 @@ impl AquaRegistry {
if let Some(version_filter) = &pkg.version_filter {
pkg.version_filter_expr = Some(expr::compile(version_filter)?);
}
CACHE.lock().await.insert(id.to_string(), pkg.clone());
Ok(pkg)
}

Expand Down
2 changes: 1 addition & 1 deletion xtasks/test/perf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fi

recent_benchmarks["install-uncached"]=0
recent_benchmarks["install-cached"]=186
recent_benchmarks["ls-uncached"]=1003
recent_benchmarks["ls-uncached"]=700
recent_benchmarks["ls-cached"]=0
recent_benchmarks["bin-paths-uncached"]=1077
recent_benchmarks["bin-paths-cached"]=0
Expand Down
Loading