@@ -35,6 +35,10 @@ macro_rules! generate_config_setting_getter {
3535}
3636
3737impl Cache {
38+ /// Builds a [`Cache`] from the configuration and spawns the cache worker.
39+ ///
40+ /// # Errors
41+ /// Returns an error if the configuration is invalid.
3842 pub fn new ( mut config : CacheConfig ) -> Result < Self > {
3943 config. validate ( ) ?;
4044 Ok ( Self {
@@ -107,7 +111,6 @@ struct Config {
107111#[ derive( serde_derive:: Deserialize , Debug , Clone ) ]
108112#[ serde( deny_unknown_fields) ]
109113pub struct CacheConfig {
110- enabled : bool ,
111114 directory : Option < PathBuf > ,
112115 #[ serde(
113116 default = "default_worker_event_queue_size" ,
@@ -175,6 +178,28 @@ pub struct CacheConfig {
175178 files_total_size_limit_percent_if_deleting : u8 ,
176179}
177180
181+ impl Default for CacheConfig {
182+ fn default ( ) -> Self {
183+ Self {
184+ directory : None ,
185+ worker_event_queue_size : default_worker_event_queue_size ( ) ,
186+ baseline_compression_level : default_baseline_compression_level ( ) ,
187+ optimized_compression_level : default_optimized_compression_level ( ) ,
188+ optimized_compression_usage_counter_threshold :
189+ default_optimized_compression_usage_counter_threshold ( ) ,
190+ cleanup_interval : default_cleanup_interval ( ) ,
191+ optimizing_compression_task_timeout : default_optimizing_compression_task_timeout ( ) ,
192+ allowed_clock_drift_for_files_from_future :
193+ default_allowed_clock_drift_for_files_from_future ( ) ,
194+ file_count_soft_limit : default_file_count_soft_limit ( ) ,
195+ files_total_size_soft_limit : default_files_total_size_soft_limit ( ) ,
196+ file_count_limit_percent_if_deleting : default_file_count_limit_percent_if_deleting ( ) ,
197+ files_total_size_limit_percent_if_deleting :
198+ default_files_total_size_limit_percent_if_deleting ( ) ,
199+ }
200+ }
201+ }
202+
178203/// Creates a new configuration file at specified path, or default path if None is passed.
179204/// Fails if file already exists.
180205pub fn create_new_config < P : AsRef < Path > + Debug > ( config_file : Option < P > ) -> Result < PathBuf > {
@@ -209,7 +234,6 @@ pub fn create_new_config<P: AsRef<Path> + Debug>(config_file: Option<P>) -> Resu
209234# https://bytecodealliance.github.io/wasmtime/cli-cache.html
210235
211236[cache]
212- enabled = true
213237" ;
214238
215239 fs:: write ( & config_file, content) . with_context ( || {
@@ -383,58 +407,9 @@ macro_rules! generate_setting_getter {
383407}
384408
385409impl CacheConfig {
386- generate_setting_getter ! ( worker_event_queue_size: u64 ) ;
387- generate_setting_getter ! ( baseline_compression_level: i32 ) ;
388- generate_setting_getter ! ( optimized_compression_level: i32 ) ;
389- generate_setting_getter ! ( optimized_compression_usage_counter_threshold: u64 ) ;
390- generate_setting_getter ! ( cleanup_interval: Duration ) ;
391- generate_setting_getter ! ( optimizing_compression_task_timeout: Duration ) ;
392- generate_setting_getter ! ( allowed_clock_drift_for_files_from_future: Duration ) ;
393- generate_setting_getter ! ( file_count_soft_limit: u64 ) ;
394- generate_setting_getter ! ( files_total_size_soft_limit: u64 ) ;
395- generate_setting_getter ! ( file_count_limit_percent_if_deleting: u8 ) ;
396- generate_setting_getter ! ( files_total_size_limit_percent_if_deleting: u8 ) ;
397-
398- /// Returns true if and only if the cache is enabled.
399- pub fn enabled ( & self ) -> bool {
400- self . enabled
401- }
402-
403- /// Returns path to the cache directory.
404- ///
405- /// Panics if the cache is disabled.
406- pub fn directory ( & self ) -> & PathBuf {
407- self . directory
408- . as_ref ( )
409- . expect ( CACHE_IMPROPER_CONFIG_ERROR_MSG )
410- }
411-
412410 /// Creates a new set of configuration which represents a disabled cache
413- pub fn new_cache_disabled ( ) -> Self {
414- Self {
415- enabled : false ,
416- directory : None ,
417- worker_event_queue_size : default_worker_event_queue_size ( ) ,
418- baseline_compression_level : default_baseline_compression_level ( ) ,
419- optimized_compression_level : default_optimized_compression_level ( ) ,
420- optimized_compression_usage_counter_threshold :
421- default_optimized_compression_usage_counter_threshold ( ) ,
422- cleanup_interval : default_cleanup_interval ( ) ,
423- optimizing_compression_task_timeout : default_optimizing_compression_task_timeout ( ) ,
424- allowed_clock_drift_for_files_from_future :
425- default_allowed_clock_drift_for_files_from_future ( ) ,
426- file_count_soft_limit : default_file_count_soft_limit ( ) ,
427- files_total_size_soft_limit : default_files_total_size_soft_limit ( ) ,
428- file_count_limit_percent_if_deleting : default_file_count_limit_percent_if_deleting ( ) ,
429- files_total_size_limit_percent_if_deleting :
430- default_files_total_size_limit_percent_if_deleting ( ) ,
431- }
432- }
433-
434- fn new_cache_enabled_template ( ) -> Self {
435- let mut conf = Self :: new_cache_disabled ( ) ;
436- conf. enabled = true ;
437- conf
411+ pub fn new ( ) -> Self {
412+ Self :: default ( )
438413 }
439414
440415 /// Loads cache configuration specified at `path`.
@@ -455,12 +430,6 @@ impl CacheConfig {
455430 ///
456431 /// [docs]: https://bytecodealliance.github.io/wasmtime/cli-cache.html
457432 pub fn from_file ( config_file : Option < & Path > ) -> Result < Self > {
458- let mut config = Self :: load_and_parse_file ( config_file) ?;
459- config. validate ( ) ?;
460- Ok ( config)
461- }
462-
463- fn load_and_parse_file ( config_file : Option < & Path > ) -> Result < Self > {
464433 // get config file path
465434 let ( config_file, user_custom_file) = match config_file {
466435 Some ( path) => ( path. to_path_buf ( ) , true ) ,
@@ -470,7 +439,7 @@ impl CacheConfig {
470439 // read config, or use default one
471440 let entity_exists = config_file. exists ( ) ;
472441 match ( entity_exists, user_custom_file) {
473- ( false , false ) => Ok ( Self :: new_cache_enabled_template ( ) ) ,
442+ ( false , false ) => Ok ( Self :: new ( ) ) ,
474443 _ => {
475444 let contents = fs:: read_to_string ( & config_file) . context ( format ! (
476445 "failed to read config file: {}" ,
@@ -485,170 +454,6 @@ impl CacheConfig {
485454 }
486455 }
487456
488- /// validate values and fill in defaults
489- fn validate ( & mut self ) -> Result < ( ) > {
490- self . validate_directory_or_default ( ) ?;
491- self . validate_worker_event_queue_size ( ) ;
492- self . validate_baseline_compression_level ( ) ?;
493- self . validate_optimized_compression_level ( ) ?;
494- self . validate_file_count_limit_percent_if_deleting ( ) ?;
495- self . validate_files_total_size_limit_percent_if_deleting ( ) ?;
496- Ok ( ( ) )
497- }
498-
499- fn validate_directory_or_default ( & mut self ) -> Result < ( ) > {
500- if self . directory . is_none ( ) {
501- match project_dirs ( ) {
502- Some ( proj_dirs) => self . directory = Some ( proj_dirs. cache_dir ( ) . to_path_buf ( ) ) ,
503- None => {
504- bail ! ( "Cache directory not specified and failed to get the default" ) ;
505- }
506- }
507- }
508-
509- // On Windows, if we want long paths, we need '\\?\' prefix, but it doesn't work
510- // with relative paths. One way to get absolute path (the only one?) is to use
511- // fs::canonicalize, but it requires that given path exists. The extra advantage
512- // of this method is fact that the method prepends '\\?\' on Windows.
513- let cache_dir = self . directory . as_ref ( ) . unwrap ( ) ;
514-
515- if !cache_dir. is_absolute ( ) {
516- bail ! (
517- "Cache directory path has to be absolute, path: {}" ,
518- cache_dir. display( ) ,
519- ) ;
520- }
521-
522- fs:: create_dir_all ( cache_dir) . context ( format ! (
523- "failed to create cache directory: {}" ,
524- cache_dir. display( )
525- ) ) ?;
526- let canonical = fs:: canonicalize ( cache_dir) . context ( format ! (
527- "failed to canonicalize cache directory: {}" ,
528- cache_dir. display( )
529- ) ) ?;
530- self . directory = Some ( canonical) ;
531- Ok ( ( ) )
532- }
533-
534- fn validate_worker_event_queue_size ( & self ) {
535- if self . worker_event_queue_size < worker_event_queue_size_warning_threshold ( ) {
536- warn ! ( "Detected small worker event queue size. Some messages might be lost." ) ;
537- }
538- }
539-
540- fn validate_baseline_compression_level ( & self ) -> Result < ( ) > {
541- if !ZSTD_COMPRESSION_LEVELS . contains ( & self . baseline_compression_level ) {
542- bail ! (
543- "Invalid baseline compression level: {} not in {:#?}" ,
544- self . baseline_compression_level,
545- ZSTD_COMPRESSION_LEVELS
546- ) ;
547- }
548- Ok ( ( ) )
549- }
550-
551- // assumption: baseline compression level has been verified
552- fn validate_optimized_compression_level ( & self ) -> Result < ( ) > {
553- if !ZSTD_COMPRESSION_LEVELS . contains ( & self . optimized_compression_level ) {
554- bail ! (
555- "Invalid optimized compression level: {} not in {:#?}" ,
556- self . optimized_compression_level,
557- ZSTD_COMPRESSION_LEVELS
558- ) ;
559- }
560-
561- if self . optimized_compression_level < self . baseline_compression_level {
562- bail ! (
563- "Invalid optimized compression level is lower than baseline: {} < {}" ,
564- self . optimized_compression_level,
565- self . baseline_compression_level
566- ) ;
567- }
568- Ok ( ( ) )
569- }
570-
571- fn validate_file_count_limit_percent_if_deleting ( & self ) -> Result < ( ) > {
572- if self . file_count_limit_percent_if_deleting > 100 {
573- bail ! (
574- "Invalid files count limit percent if deleting: {} not in range 0-100%" ,
575- self . file_count_limit_percent_if_deleting
576- ) ;
577- }
578- Ok ( ( ) )
579- }
580-
581- fn validate_files_total_size_limit_percent_if_deleting ( & self ) -> Result < ( ) > {
582- if self . files_total_size_limit_percent_if_deleting > 100 {
583- bail ! (
584- "Invalid files total size limit percent if deleting: {} not in range 0-100%" ,
585- self . files_total_size_limit_percent_if_deleting
586- ) ;
587- }
588- Ok ( ( ) )
589- }
590-
591- /// Builds a [`Cache`] from the configuration and spawns the cache worker.
592- ///
593- /// # Errors
594- /// Returns an error if the configuration is invalid.
595- pub fn spawn ( & mut self ) -> Result < Cache > {
596- self . validate ( ) ?;
597-
598- // Unwrapping because validation will ensure these are all set
599- Ok ( Cache {
600- config : self . clone ( ) ,
601- worker : Worker :: start_new ( self ) ,
602- state : Default :: default ( ) ,
603- } )
604- }
605- }
606-
607- /// A builder for `CacheConfig`s.
608- #[ derive( Debug , Clone ) ]
609- pub struct CacheConfigBuilder {
610- directory : Option < PathBuf > ,
611- worker_event_queue_size : u64 ,
612- baseline_compression_level : i32 ,
613- optimized_compression_level : i32 ,
614- optimized_compression_usage_counter_threshold : u64 ,
615- optimizing_compression_task_timeout : Duration ,
616- cleanup_interval : Duration ,
617- allowed_clock_drift_for_files_from_future : Duration ,
618- file_count_soft_limit : u64 ,
619- files_total_size_soft_limit : u64 ,
620- file_count_limit_percent_if_deleting : u8 ,
621- files_total_size_limit_percent_if_deleting : u8 ,
622- }
623-
624- impl Default for CacheConfigBuilder {
625- fn default ( ) -> Self {
626- Self {
627- directory : None ,
628- worker_event_queue_size : default_worker_event_queue_size ( ) ,
629- baseline_compression_level : default_baseline_compression_level ( ) ,
630- optimized_compression_level : default_optimized_compression_level ( ) ,
631- optimized_compression_usage_counter_threshold :
632- default_optimized_compression_usage_counter_threshold ( ) ,
633- optimizing_compression_task_timeout : default_optimizing_compression_task_timeout ( ) ,
634- cleanup_interval : default_cleanup_interval ( ) ,
635- allowed_clock_drift_for_files_from_future :
636- default_allowed_clock_drift_for_files_from_future ( ) ,
637- file_count_soft_limit : default_file_count_soft_limit ( ) ,
638- files_total_size_soft_limit : default_files_total_size_soft_limit ( ) ,
639- file_count_limit_percent_if_deleting : default_file_count_limit_percent_if_deleting ( ) ,
640- files_total_size_limit_percent_if_deleting :
641- default_files_total_size_limit_percent_if_deleting ( ) ,
642- }
643- }
644- }
645-
646- impl CacheConfigBuilder {
647- /// Specify where the cache directory is. Must be an absolute path.
648- pub fn new ( ) -> Self {
649- Self :: default ( )
650- }
651-
652457 generate_setting_getter ! ( worker_event_queue_size: u64 ) ;
653458 generate_setting_getter ! ( baseline_compression_level: i32 ) ;
654459 generate_setting_getter ! ( optimized_compression_level: i32 ) ;
@@ -663,7 +468,7 @@ impl CacheConfigBuilder {
663468
664469 /// Returns path to the cache directory.
665470 ///
666- /// Panics if the directory hasn't been set yet .
471+ /// Panics if the cache is disabled .
667472 pub fn directory ( & self ) -> & PathBuf {
668473 self . directory
669474 . as_ref ( )
@@ -790,7 +595,7 @@ impl CacheConfigBuilder {
790595
791596 /// validate values and fill in defaults
792597 fn validate ( & mut self ) -> Result < ( ) > {
793- self . validate_directory ( ) ?;
598+ self . validate_directory_or_default ( ) ?;
794599 self . validate_worker_event_queue_size ( ) ;
795600 self . validate_baseline_compression_level ( ) ?;
796601 self . validate_optimized_compression_level ( ) ?;
@@ -799,7 +604,7 @@ impl CacheConfigBuilder {
799604 Ok ( ( ) )
800605 }
801606
802- fn validate_directory ( & mut self ) -> Result < ( ) > {
607+ fn validate_directory_or_default ( & mut self ) -> Result < ( ) > {
803608 if self . directory . is_none ( ) {
804609 match project_dirs ( ) {
805610 Some ( proj_dirs) => self . directory = Some ( proj_dirs. cache_dir ( ) . to_path_buf ( ) ) ,
@@ -890,44 +695,6 @@ impl CacheConfigBuilder {
890695 }
891696 Ok ( ( ) )
892697 }
893-
894- pub fn build ( mut self ) -> Result < CacheConfig > {
895- self . validate ( ) ?;
896- let CacheConfigBuilder {
897- directory,
898- worker_event_queue_size,
899- baseline_compression_level,
900- optimized_compression_level,
901- optimized_compression_usage_counter_threshold,
902- cleanup_interval,
903- optimizing_compression_task_timeout,
904- allowed_clock_drift_for_files_from_future,
905- file_count_soft_limit,
906- files_total_size_soft_limit,
907- file_count_limit_percent_if_deleting,
908- files_total_size_limit_percent_if_deleting,
909- } = self ;
910-
911- let mut config = CacheConfig {
912- enabled : true ,
913- directory,
914- worker_event_queue_size,
915- baseline_compression_level,
916- optimized_compression_level,
917- optimized_compression_usage_counter_threshold,
918- cleanup_interval,
919- optimizing_compression_task_timeout,
920- allowed_clock_drift_for_files_from_future,
921- file_count_soft_limit,
922- files_total_size_soft_limit,
923- file_count_limit_percent_if_deleting,
924- files_total_size_limit_percent_if_deleting,
925- } ;
926-
927- config. validate ( ) ?;
928-
929- Ok ( config)
930- }
931698}
932699
933700#[ cfg( test) ]
0 commit comments