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

33
src/cache/disk.rs vendored
View file

@ -196,11 +196,23 @@ 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 {
return true;
};
if error.kind() != std::io::ErrorKind::NotFound {
warn!("Failed to delete file `{}` from cache: {}", &key, error);
return false;
}
if let Ok(bytes) = hex::decode(&key) { if let Ok(bytes) = hex::decode(&key) {
if bytes.len() == 16 { if bytes.len() != 16 {
warn!("Failed to delete file `{}`; invalid hash size.", &key);
return false;
}
let hash = Md5Hash(*GenericArray::from_slice(&bytes)); let hash = Md5Hash(*GenericArray::from_slice(&bytes));
let path: PathBuf = hash.into(); let path: PathBuf = hash.into();
if let Err(e) = remove_file(&path).await { if let Err(e) = remove_file(&path).await {
@ -213,24 +225,11 @@ async fn remove_file_handler(key: String) -> bool {
} else { } else {
true true
} }
} else {
warn!("Failed to delete file `{}`; invalid hash size.", &key);
false
}
} else { } else {
warn!("Failed to delete file `{}`; not a md5hash.", &key); warn!("Failed to delete file `{}`; not a md5hash.", &key);
false false
} }
} }
_ => {
warn!("Failed to delete file `{}` from cache: {}", &key, e);
false
}
}
} else {
true
}
}
#[instrument(level = "debug", skip(transaction))] #[instrument(level = "debug", skip(transaction))]
async fn handle_db_get(entry: &Path, transaction: &mut Transaction<'_, Sqlite>) { async fn handle_db_get(entry: &Path, transaction: &mut Transaction<'_, Sqlite>) {

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])