Skip to content

Commit c61081d

Browse files
committed
Actually fill toolchain cache
1 parent f60ff25 commit c61081d

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/shim/src/toolchain.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use dkregistry::v2::manifest::{Manifest, RuntimeConfig};
55
use std::{
66
collections::{BTreeMap, HashMap},
77
path::{Path, PathBuf},
8+
sync::Mutex,
89
};
910

1011
#[derive(Clone)]
@@ -58,7 +59,7 @@ pub struct ToolchainPuller {
5859
// TODO: per-registry options
5960
disable_tls: bool,
6061
/// Cache for already pulled toolchains.
61-
cache: HashMap<String, PulledToolchain>,
62+
cache: Mutex<HashMap<String, PulledToolchain>>,
6263
}
6364

6465
impl ToolchainPuller {
@@ -76,7 +77,7 @@ impl ToolchainPuller {
7677
image_puller,
7778
toolchains_dir: tooclhains_dir.to_path_buf(),
7879
disable_tls,
79-
cache: HashMap::new(),
80+
cache: Mutex::new(HashMap::new()),
8081
})
8182
}
8283

@@ -153,8 +154,13 @@ impl ToolchainPuller {
153154

154155
#[tracing::instrument(skip(self))]
155156
pub async fn resolve(&self, toolchain_image: &str) -> anyhow::Result<PulledToolchain> {
156-
if let Some(info) = self.cache.get(toolchain_image) {
157-
return Ok(info.clone());
157+
{
158+
// TODO: do not pull one image concurrently, instead one task should pull
159+
// ond other should wait.
160+
let cache = self.cache.lock().unwrap();
161+
if let Some(info) = cache.get(toolchain_image) {
162+
return Ok(info.clone());
163+
}
158164
}
159165
let dirname = base64::encode(toolchain_image);
160166
let toolchain_dir = self.toolchains_dir.join(&dirname);
@@ -164,9 +170,14 @@ impl ToolchainPuller {
164170
.await
165171
.context("toolchain download error")?;
166172

167-
Ok(PulledToolchain {
173+
let pt = PulledToolchain {
168174
path: dirname,
169175
image_config,
170-
})
176+
};
177+
{
178+
let mut cache = self.cache.lock().unwrap();
179+
cache.insert(toolchain_image.to_string(), pt.clone());
180+
}
181+
Ok(pt)
171182
}
172183
}

0 commit comments

Comments
 (0)