@@ -39,6 +39,7 @@ pub(crate) use native_http1_cache_backend::prepare_native_storage_bin_root_for_l
3939use native_http1_cache_backend:: {
4040 NativeDiskCacheBackend , NativeDiskCacheLocation , NativeDiskCacheRecord , NativeDiskCacheState ,
4141} ;
42+ pub ( crate ) use native_http1_cache_disk_path:: prepare_native_disk_cache_root;
4243use native_http1_cache_disk_path:: {
4344 native_cache_path_contains_symlink, native_disk_cache_read_limit, read_native_disk_cache_file,
4445} ;
@@ -103,6 +104,13 @@ pub(crate) struct NativeDiskCache {
103104 index_flush : Option < NativeStorageBinIndexFlush > ,
104105}
105106
107+ impl Drop for NativeDiskCache {
108+ fn drop ( & mut self ) {
109+ // Close background writes and checkpoint while the backend still owns its lease.
110+ drop ( self . index_flush . take ( ) ) ;
111+ }
112+ }
113+
106114#[ derive( Clone , Copy , Debug , Default , Eq , PartialEq ) ]
107115pub ( crate ) struct NativeDiskCacheStats {
108116 pub ( crate ) entries : u64 ,
@@ -125,34 +133,26 @@ impl NativeDiskCache {
125133 }
126134
127135 pub ( crate ) fn from_config ( config : & CacheConfig ) -> Option < Self > {
128- if !config. disk . enabled || !native_disk_cache_supported ( config) {
129- return None ;
136+ Self :: try_from_config ( config) . ok ( ) . flatten ( )
137+ }
138+
139+ pub ( crate ) fn try_from_config ( config : & CacheConfig ) -> std:: io:: Result < Option < Self > > {
140+ if !config. disk . enabled {
141+ return Ok ( None ) ;
130142 }
131- let ( root, backend) = match NativeDiskCacheBackend :: from_config ( config) {
132- Ok ( backend) => backend,
133- Err ( error) => {
134- log:: error!(
135- target: "fluxheim::native_http1" ,
136- "native disk cache backend: {error}"
137- ) ;
138- return None ;
139- }
140- } ;
141- let encryption =
142- match NativeDiskCacheEncryption :: from_config ( & config. disk . encryption , & root) {
143- Ok ( encryption) => encryption,
144- Err ( error) => {
145- log:: error!(
146- target: "fluxheim::native_http1" ,
147- "native disk cache encryption {}: {error}" ,
148- root. display( )
149- ) ;
150- fluxheim_observability:: operational:: failure (
151- fluxheim_observability:: event:: FailureReason :: CacheEncryption ,
152- ) ;
153- return None ;
154- }
155- } ;
143+ if !native_disk_cache_supported ( config) {
144+ return Err ( std:: io:: Error :: new (
145+ std:: io:: ErrorKind :: Unsupported ,
146+ "unsupported disk cache policy" ,
147+ ) ) ;
148+ }
149+ let ( root, backend) = NativeDiskCacheBackend :: from_config ( config) ?;
150+ let encryption = NativeDiskCacheEncryption :: from_config ( & config. disk . encryption , & root)
151+ . inspect_err ( |_| {
152+ fluxheim_observability:: operational:: failure (
153+ fluxheim_observability:: event:: FailureReason :: CacheEncryption ,
154+ )
155+ } ) ?;
156156 let mut cache = Self {
157157 root,
158158 max_bytes : config. disk . max_size_bytes . as_u64 ( ) ,
@@ -175,36 +175,18 @@ impl NativeDiskCache {
175175 . ok_or_else ( || std:: io:: Error :: other ( "cache encryption disappeared" ) ) ?
176176 . complete_root_migration ( & cache. root )
177177 } ) ;
178- if let Err ( error) = migration {
179- log:: error!(
180- target: "fluxheim::security" ,
181- "native disk cache encryption-root migration {}: {error}" ,
182- cache. root. display( )
183- ) ;
184- return None ;
185- }
186- }
187- if let Err ( error) = cache. rebuild_index ( ) {
188- log:: warn!(
189- target: "fluxheim::native_http1" ,
190- "native disk cache index rebuild {}: {error}" ,
191- cache. root. display( )
192- ) ;
178+ migration?;
193179 }
180+ cache. rebuild_index ( ) ?;
194181 cache. index_flush = NativeStorageBinIndexFlush :: start (
195182 & cache. backend ,
196183 & cache. state ,
197184 cache
198185 . encryption
199186 . as_ref ( )
200187 . map ( NativeDiskCacheEncryption :: index_key) ,
201- )
202- . inspect_err ( |error| {
203- log:: error!( target: "fluxheim::native_http1" , "cache index service: {error}" ) ;
204- } )
205- . ok ( ) ?;
206- cache. persist_storage_bin_index ( ) ;
207- Some ( cache)
188+ ) ?;
189+ Ok ( Some ( cache) )
208190 }
209191
210192 pub ( crate ) fn stats ( & self ) -> NativeDiskCacheStats {
0 commit comments