This commit is contained in:
Edward Shen 2021-07-15 19:13:31 -04:00
parent 87271c85a7
commit 041760f9e9
Signed by: edward
GPG key ID: 19182661E818369F
3 changed files with 43 additions and 46 deletions

61
src/cache/disk.rs vendored
View file

@ -196,39 +196,38 @@ async fn db_listener(
/// Returns if a file was successfully deleted. /// Returns if a file was successfully deleted.
async fn remove_file_handler(key: String) -> bool { async fn remove_file_handler(key: String) -> bool {
if let Err(e) = remove_file(&key).await { let error = if let Err(e) = remove_file(&key).await {
match e.kind() { e
std::io::ErrorKind::NotFound => { } else {
if let Ok(bytes) = hex::decode(&key) { return true;
if bytes.len() == 16 { };
let hash = Md5Hash(*GenericArray::from_slice(&bytes));
let path: PathBuf = hash.into(); if error.kind() != std::io::ErrorKind::NotFound {
if let Err(e) = remove_file(&path).await { warn!("Failed to delete file `{}` from cache: {}", &key, error);
warn!( return false;
"Failed to delete file `{}` from cache: {}", }
path.to_string_lossy(),
e if let Ok(bytes) = hex::decode(&key) {
); if bytes.len() != 16 {
false warn!("Failed to delete file `{}`; invalid hash size.", &key);
} else { return false;
true }
}
} else { let hash = Md5Hash(*GenericArray::from_slice(&bytes));
warn!("Failed to delete file `{}`; invalid hash size.", &key); let path: PathBuf = hash.into();
false if let Err(e) = remove_file(&path).await {
} warn!(
} else { "Failed to delete file `{}` from cache: {}",
warn!("Failed to delete file `{}`; not a md5hash.", &key); path.to_string_lossy(),
false e
} );
} false
_ => { } else {
warn!("Failed to delete file `{}` from cache: {}", &key, e); true
false
}
} }
} else { } else {
true warn!("Failed to delete file `{}`; not a md5hash.", &key);
false
} }
} }

View file

@ -73,11 +73,11 @@ pub fn load_config() -> Result<Config, ConfigError> {
Ordering::Release, Ordering::Release,
); );
config.proxy.clone().map(|socket| { if let Some(socket) = config.proxy.clone() {
USE_PROXY USE_PROXY
.set(socket) .set(socket)
.expect("USE_PROXY to be set only by this function"); .expect("USE_PROXY to be set only by this function");
}); }
DISABLE_CERT_VALIDATION.store( DISABLE_CERT_VALIDATION.store(
config config

View file

@ -104,20 +104,20 @@ pub async fn load_geo_ip_data(license_key: ClientSecret) -> Result<(), DbLoadErr
// Check date of db // Check date of db
let db_date_created = metadata(DB_PATH) let db_date_created = metadata(DB_PATH)
.ok() .ok()
.and_then(|metadata| match metadata.created() { .and_then(|metadata| {
Ok(time) => Some(time), if let Ok(time) = metadata.created() {
Err(_) => { Some(time)
} else {
debug("fs didn't report birth time, fall back to last modified instead"); debug("fs didn't report birth time, fall back to last modified instead");
metadata.modified().ok() metadata.modified().ok()
} }
}) })
.unwrap_or(SystemTime::UNIX_EPOCH); .unwrap_or(SystemTime::UNIX_EPOCH);
let duration = match SystemTime::now().duration_since(db_date_created) { let duration = if let Ok(time) = SystemTime::now().duration_since(db_date_created) {
Ok(time) => Duration::from_std(time).expect("duration to fit"), Duration::from_std(time).expect("duration to fit")
Err(_) => { } else {
warn!("Clock may have gone backwards?"); warn!("Clock may have gone backwards?");
Duration::max_value() Duration::max_value()
}
}; };
// DB expired, fetch a new one // DB expired, fetch a new one
@ -172,14 +172,12 @@ async fn fetch_db(license_key: ClientSecret) -> Result<(), DbLoadError> {
} }
pub fn record_country_visit(country: Option<Country>) { pub fn record_country_visit(country: Option<Country>) {
let iso_code = if let Some(country) = country { let iso_code = country.map_or("unknown", |country| {
country country
.country .country
.and_then(|c| c.iso_code) .and_then(|c| c.iso_code)
.unwrap_or("unknown") .unwrap_or("unknown")
} else { });
"unknown"
};
COUNTRY_VISIT_COUNTER COUNTRY_VISIT_COUNTER
.get_metric_with_label_values(&[iso_code]) .get_metric_with_label_values(&[iso_code])