Skip to content

Commit 61a371a

Browse files
authored
CacheConfigs are always enabled; clarify directory validation (#11376)
1 parent f7a5aa3 commit 61a371a

File tree

6 files changed

+24
-33
lines changed

6 files changed

+24
-33
lines changed

crates/cache/src/config.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -305,22 +305,17 @@ generate_deserializer!(deserialize_percent(num: u8, unit: &str) -> u8 {
305305
}
306306
});
307307

308-
static CACHE_IMPROPER_CONFIG_ERROR_MSG: &str =
309-
"Cache system should be enabled and all settings must be validated or defaulted";
310-
311308
macro_rules! generate_setting_getter {
312309
($setting:ident: $setting_type:ty) => {
313310
#[doc = concat!("Returns ", "`", stringify!($setting), "`.")]
314-
///
315-
/// Panics if the cache is disabled.
316311
pub fn $setting(&self) -> $setting_type {
317312
self.$setting
318313
}
319314
};
320315
}
321316

322317
impl CacheConfig {
323-
/// Creates a new set of configuration which represents a disabled cache
318+
/// Creates a cache configuration with default settings.
324319
pub fn new() -> Self {
325320
Self::default()
326321
}
@@ -385,13 +380,9 @@ impl CacheConfig {
385380
generate_setting_getter!(file_count_limit_percent_if_deleting: u8);
386381
generate_setting_getter!(files_total_size_limit_percent_if_deleting: u8);
387382

388-
/// Returns path to the cache directory.
389-
///
390-
/// Panics if the cache is disabled.
391-
pub fn directory(&self) -> &PathBuf {
392-
self.directory
393-
.as_ref()
394-
.expect(CACHE_IMPROPER_CONFIG_ERROR_MSG)
383+
/// Returns path to the cache directory if one is set.
384+
pub fn directory(&self) -> Option<&PathBuf> {
385+
self.directory.as_ref()
395386
}
396387

397388
/// Specify where the cache directory is. Must be an absolute path.

crates/cache/src/config/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn test_all_settings() {
9999
fn check_conf(conf: &CacheConfig, cd: &PathBuf) {
100100
assert_eq!(
101101
conf.directory(),
102-
&fs::canonicalize(cd).expect("canonicalize failed")
102+
Some(&fs::canonicalize(cd).expect("canonicalize failed"))
103103
);
104104
assert_eq!(conf.worker_event_queue_size(), 0x10);
105105
assert_eq!(conf.baseline_compression_level(), 3);
@@ -536,7 +536,7 @@ fn test_builder_all_settings() {
536536
fn check_conf(conf: &CacheConfig, cd: &PathBuf) {
537537
assert_eq!(
538538
conf.directory(),
539-
&fs::canonicalize(cd).expect("canonicalize failed")
539+
Some(&fs::canonicalize(cd).expect("canonicalize failed"))
540540
);
541541
assert_eq!(conf.worker_event_queue_size(), 0x10);
542542
assert_eq!(conf.baseline_compression_level(), 3);

crates/cache/src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ pub struct Cache {
3737
macro_rules! generate_config_setting_getter {
3838
($setting:ident: $setting_type:ty) => {
3939
#[doc = concat!("Returns ", "`", stringify!($setting), "`.")]
40-
///
41-
/// Panics if the cache is disabled.
4240
pub fn $setting(&self) -> $setting_type {
4341
self.config.$setting()
4442
}
@@ -97,10 +95,11 @@ impl Cache {
9795
generate_config_setting_getter!(files_total_size_limit_percent_if_deleting: u8);
9896

9997
/// Returns path to the cache directory.
100-
///
101-
/// Panics if the cache directory is not set.
10298
pub fn directory(&self) -> &PathBuf {
103-
&self.config.directory()
99+
&self
100+
.config
101+
.directory()
102+
.expect("directory should be validated in Config::new")
104103
}
105104

106105
#[cfg(test)]

crates/cache/src/tests.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ fn test_cache_init() {
2424

2525
// assumption: config init creates cache directory and returns canonicalized path
2626
assert_eq!(
27-
*cache_config.directory(),
28-
fs::canonicalize(cache_dir).unwrap()
27+
cache_config.directory(),
28+
Some(&fs::canonicalize(cache_dir).unwrap())
2929
);
3030
assert_eq!(
3131
cache_config.baseline_compression_level(),
@@ -53,8 +53,8 @@ fn test_write_read_cache() {
5353

5454
// assumption: config load creates cache directory and returns canonicalized path
5555
assert_eq!(
56-
*cache_config.directory(),
57-
fs::canonicalize(cache_dir).unwrap()
56+
cache_config.directory(),
57+
Some(&fs::canonicalize(cache_dir).unwrap())
5858
);
5959

6060
let compiler1 = "test-1";

crates/cache/src/worker.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,12 @@ impl WorkerThread {
393393
trace!("Task finished: recompress file: {}", path.display());
394394
}
395395

396+
fn directory(&self) -> &PathBuf {
397+
self.cache_config
398+
.directory()
399+
.expect("CacheConfig should be validated before being passed to a WorkerThread")
400+
}
401+
396402
fn handle_on_cache_update(&self, path: PathBuf) {
397403
trace!("handle_on_cache_update() for path: {}", path.display());
398404

@@ -416,7 +422,7 @@ impl WorkerThread {
416422
// acquire lock for cleanup task
417423
// Lock is a proof of recent cleanup task, so we don't want to delete them.
418424
// Expired locks will be deleted by the cleanup task.
419-
let cleanup_file = self.cache_config.directory().join(".cleanup"); // some non existing marker file
425+
let cleanup_file = self.directory().join(".cleanup"); // some non existing marker file
420426
if acquire_task_fs_lock(
421427
&cleanup_file,
422428
self.cache_config.cleanup_interval(),
@@ -722,12 +728,7 @@ impl WorkerThread {
722728
}
723729

724730
let mut vec = Vec::new();
725-
enter_dir(
726-
&mut vec,
727-
self.cache_config.directory(),
728-
0,
729-
&self.cache_config,
730-
);
731+
enter_dir(&mut vec, self.directory(), 0, &self.cache_config);
731732
vec
732733
}
733734
}

crates/wasmtime/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,8 +1398,8 @@ impl Config {
13981398

13991399
/// Set a custom [`Cache`].
14001400
///
1401-
/// To load a cache from a file, use [`Cache::from_file`]. Otherwise, you can create a new
1402-
/// cache config using [`CacheConfig::new`] and passing that to [`Cache::new`].
1401+
/// To load a cache configuration from a file, use [`Cache::from_file`]. Otherwise, you can
1402+
/// create a new cache config using [`CacheConfig::new`] and passing that to [`Cache::new`].
14031403
///
14041404
/// If you want to disable the cache, you can call this method with `None`.
14051405
///

0 commit comments

Comments
 (0)