From 155c4723931233620665ecc143396ea7d8ce4b3b Mon Sep 17 00:00:00 2001 From: itowlson Date: Wed, 15 Nov 2023 09:22:44 +1300 Subject: [PATCH] Allow hosts to control local loader download cache Signed-off-by: itowlson --- crates/loader/src/lib.rs | 3 ++- crates/loader/src/local.rs | 8 ++++++-- crates/loader/tests/ui.rs | 1 + crates/oci/src/client.rs | 1 + src/commands/up.rs | 2 +- 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/crates/loader/src/lib.rs b/crates/loader/src/lib.rs index 86e625d79f..9b5d5f72b1 100644 --- a/crates/loader/src/lib.rs +++ b/crates/loader/src/lib.rs @@ -30,10 +30,11 @@ pub(crate) const MAX_FILE_LOADING_CONCURRENCY: usize = 16; pub async fn from_file( manifest_path: impl AsRef, files_mount_strategy: FilesMountStrategy, + cache_root: Option, ) -> Result { let path = manifest_path.as_ref(); let app_root = parent_dir(path)?; - let loader = LocalLoader::new(&app_root, files_mount_strategy).await?; + let loader = LocalLoader::new(&app_root, files_mount_strategy, cache_root).await?; loader.load_file(path).await } diff --git a/crates/loader/src/local.rs b/crates/loader/src/local.rs index e0114c166f..ddea5ab7ab 100644 --- a/crates/loader/src/local.rs +++ b/crates/loader/src/local.rs @@ -25,14 +25,18 @@ pub struct LocalLoader { } impl LocalLoader { - pub async fn new(app_root: &Path, files_mount_strategy: FilesMountStrategy) -> Result { + pub async fn new( + app_root: &Path, + files_mount_strategy: FilesMountStrategy, + cache_root: Option, + ) -> Result { let app_root = app_root .canonicalize() .with_context(|| format!("Invalid manifest dir `{}`", app_root.display()))?; Ok(Self { app_root, files_mount_strategy, - cache: Cache::new(None).await?, + cache: Cache::new(cache_root).await?, // Limit concurrency to avoid hitting system resource limits file_loading_permits: Semaphore::new(crate::MAX_FILE_LOADING_CONCURRENCY), }) diff --git a/crates/loader/tests/ui.rs b/crates/loader/tests/ui.rs index 8dc80d2858..2debf28203 100644 --- a/crates/loader/tests/ui.rs +++ b/crates/loader/tests/ui.rs @@ -49,6 +49,7 @@ fn run_test(input: &Path, normalizer: &mut Normalizer) -> Result let locked = spin_loader::from_file( input, spin_loader::FilesMountStrategy::Copy(files_mount_root), + None, ) .await .map_err(|err| format!("{err:?}"))?; diff --git a/crates/oci/src/client.rs b/crates/oci/src/client.rs index ffa1ce86c3..8bdb58a432 100644 --- a/crates/oci/src/client.rs +++ b/crates/oci/src/client.rs @@ -81,6 +81,7 @@ impl Client { let locked = spin_loader::from_file( manifest_path, FilesMountStrategy::Copy(working_dir.path().into()), + None, ) .await?; diff --git a/src/commands/up.rs b/src/commands/up.rs index 05bfe88c9d..a36e10d5a1 100644 --- a/src/commands/up.rs +++ b/src/commands/up.rs @@ -334,7 +334,7 @@ impl UpCommand { } else { FilesMountStrategy::Copy(working_dir.join("assets")) }; - spin_loader::from_file(&manifest_path, files_mount_strategy) + spin_loader::from_file(&manifest_path, files_mount_strategy, None) .await .with_context(|| format!("Failed to load manifest from {manifest_path:?}")) }