vw_small

Hardened fork of Vaultwarden (https://github.com/dani-garcia/vaultwarden) with fewer features.
git clone https://git.philomathiclife.com/repos/vw_small
Log | Files | Refs | README

commit a55c048a622829bd91865001b879c33845f4d7ae
parent 848cd1dbec7ecbe6df3928bcdac9b332532d1748
Author: Steven Noonan <steven@uplinklabs.net>
Date:   Tue, 18 Dec 2018 13:33:32 -0800

icons: implement positive/negative cache TTLs

Signed-off-by: Steven Noonan <steven@uplinklabs.net>

Diffstat:
Msrc/api/icons.rs | 56+++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 55 insertions(+), 1 deletion(-)

diff --git a/src/api/icons.rs b/src/api/icons.rs @@ -1,5 +1,7 @@ use std::io::prelude::*; -use std::fs::{create_dir_all, File}; +use std::fs::{symlink_metadata, create_dir_all, remove_file, File}; +use std::time::SystemTime; +use std::error::Error; use rocket::Route; use rocket::response::Content; @@ -44,12 +46,23 @@ fn get_icon (domain: &str) -> Vec<u8> { }, Err(e) => { error!("Error downloading icon: {:?}", e); + mark_negcache(&path); get_fallback_icon() } } } fn get_cached_icon(path: &str) -> Option<Vec<u8>> { + // Check for expiration of negatively cached copy + if icon_is_negcached(path) { + return Some(get_fallback_icon()); + } + + // Check for expiration of successfully cached copy + if icon_is_expired(path) { + return None + } + // Try to read the cached icon, and return it if it exists if let Ok(mut f) = File::open(path) { let mut buffer = Vec::new(); @@ -62,6 +75,47 @@ fn get_cached_icon(path: &str) -> Option<Vec<u8>> { None } +fn file_is_expired(path: &str, ttl: u64) -> Result<bool, Box<Error>> { + let meta = symlink_metadata(path)?; + let modified = meta.modified()?; + let age = SystemTime::now().duration_since(modified)?; + + Ok(ttl > 0 && ttl <= age.as_secs()) +} + +fn icon_is_negcached(path: &str) -> bool { + let miss_indicator = path.to_owned() + ".miss"; + let expired = file_is_expired(&miss_indicator, CONFIG.icon_cache_negttl); + match expired { + // No longer negatively cached, drop the marker + Ok(true) => { + match remove_file(&miss_indicator) { + Ok(_) => {}, + Err(e) => { + error!("Could not remove negative cache indicator for icon {:?}: {:?}", path, e); + } + } + false + }, + + // The marker hasn't expired yet. + Ok(false) => { true } + + // The marker is missing or inaccessible in some way. + Err(_) => { false } + } +} + +fn mark_negcache(path: &str) { + let miss_indicator = path.to_owned() + ".miss"; + File::create(&miss_indicator).expect("Error creating negative cache marker"); +} + +fn icon_is_expired(path: &str) -> bool { + let expired = file_is_expired(path, CONFIG.icon_cache_ttl); + expired.unwrap_or(true) +} + fn get_icon_url(domain: &str) -> String { if CONFIG.local_icon_extractor { format!("http://{}/favicon.ico", domain)