Skip to content

Commit f50b9f9

Browse files
committed
Extract record_cache_event & skip mail caching
Fixes `[DEBUG] Failed to record cache miss for mailto:bob@example.com: URL is missing a host`
1 parent cf2bf5b commit f50b9f9

2 files changed

Lines changed: 26 additions & 38 deletions

File tree

lychee-bin/src/commands/check.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -268,23 +268,11 @@ async fn handle(
268268
Status::from_cache_status(v.value().status, &accept)
269269
};
270270

271-
// Track cache hit in the per-host stats (only for network URIs)
272-
if !uri.is_file()
273-
&& let Err(e) = client.host_pool().record_cache_hit(&uri)
274-
{
275-
log::debug!("Failed to record cache hit for {uri}: {e}");
276-
}
277-
271+
client.host_pool().record_cache_hit(&uri);
278272
return Ok(Response::new(uri.clone(), status, request.source.into()));
279273
}
280274

281-
// Cache miss - track it and run a normal check (only for network URIs)
282-
if !uri.is_file()
283-
&& let Err(e) = client.host_pool().record_cache_miss(&uri)
284-
{
285-
log::debug!("Failed to record cache miss for {uri}: {e}");
286-
}
287-
275+
client.host_pool().record_cache_miss(&uri);
288276
let response = check_url(client, request).await;
289277

290278
// - Never cache filesystem access as it is fast already so caching has no benefit.

lychee-lib/src/ratelimit/pool.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use reqwest::{Client, Request, Response};
44
use std::collections::HashMap;
55
use std::sync::Arc;
66

7-
use crate::ratelimit::{Host, HostConfigs, HostKey, HostStats, RateLimitConfig};
7+
use crate::ratelimit::{self, Host, HostConfigs, HostKey, HostStats, RateLimitConfig};
88
use crate::types::Result;
99
use 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

Comments
 (0)