@@ -4,7 +4,7 @@ use reqwest::{Client, Request, Response};
44use std:: collections:: HashMap ;
55use std:: sync:: Arc ;
66
7- use crate :: ratelimit:: { Host , HostConfigs , HostKey , HostStats , RateLimitConfig } ;
7+ use crate :: ratelimit:: { self , Host , HostConfigs , HostKey , HostStats , RateLimitConfig } ;
88use crate :: types:: Result ;
99use crate :: { CacheStatus , ErrorKind , Status , Uri } ;
1010
@@ -228,40 +228,40 @@ impl HostPool {
228228 . collect ( )
229229 }
230230
231- /// Record a cache hit for the given URI in host statistics
231+ /// Try to record a cache hit for the given URI in host statistics.
232+ /// File and mail URIs are ignored.
232233 ///
233234 /// This tracks that a request was served from the persistent disk cache
234235 /// rather than going through the rate-limited HTTP request flow.
235236 /// This method will create a host instance if one doesn't exist yet.
236- ///
237- /// # Errors
238- ///
239- /// Returns an error if the host key cannot be parsed from the URI.
240- pub fn record_cache_hit ( & self , uri : & crate :: Uri ) -> Result < ( ) > {
241- let host_key = crate :: ratelimit:: HostKey :: try_from ( uri) ?;
242-
243- // Get or create the host (this ensures statistics tracking even for cache-only requests)
244- let host = self . get_or_create_host ( host_key) ;
245- host. record_persistent_cache_hit ( ) ;
246- Ok ( ( ) )
237+ pub fn record_cache_hit ( & self , uri : & crate :: Uri ) {
238+ self . record_cache_event ( uri, |host| host. record_persistent_cache_hit ( ) ) ;
247239 }
248240
249- /// Record a cache miss for the given URI in host statistics
241+ /// Try to record a cache miss for the given URI in host statistics
242+ /// File and mail URIs are ignored.
250243 ///
251244 /// This tracks that a request could not be served from the persistent disk cache
252245 /// and will need to go through the rate-limited HTTP request flow.
253246 /// This method will create a Host instance if one doesn't exist yet.
254- ///
255- /// # Errors
256- ///
257- /// Returns an error if the host key cannot be parsed from the URI.
258- pub fn record_cache_miss ( & self , uri : & crate :: Uri ) -> Result < ( ) > {
259- let host_key = crate :: ratelimit:: HostKey :: try_from ( uri) ?;
247+ pub fn record_cache_miss ( & self , uri : & Uri ) {
248+ self . record_cache_event ( uri, |host| host. record_persistent_cache_miss ( ) ) ;
249+ }
260250
261- // Get or create the host (this ensures statistics tracking even for cache-only requests)
262- let host = self . get_or_create_host ( host_key) ;
263- host. record_persistent_cache_miss ( ) ;
264- Ok ( ( ) )
251+ /// File and mail URIs are ignored.
252+ /// Errors are logged and not returned.
253+ fn record_cache_event < F : Fn ( Arc < Host > ) > ( & self , uri : & Uri , action : F ) {
254+ if !uri. is_file ( ) && !uri. is_mail ( ) {
255+ match ratelimit:: HostKey :: try_from ( uri) {
256+ Ok ( key) => {
257+ let host = self . get_or_create_host ( key) ;
258+ action ( host) ;
259+ }
260+ Err ( e) => {
261+ log:: debug!( "Failed to record cache hit for {uri}: {e}" ) ;
262+ }
263+ }
264+ }
265265 }
266266}
267267
0 commit comments