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.
async fn remove_file_handler(key: String) -> bool {
if let Err(e) = remove_file(&key).await {
match e.kind() {
std::io::ErrorKind::NotFound => {
let error = if let Err(e) = remove_file(&key).await {
e
} 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 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 path: PathBuf = hash.into();
if let Err(e) = remove_file(&path).await {
@ -213,24 +225,11 @@ async fn remove_file_handler(key: String) -> bool {
} else {
true
}
} else {
warn!("Failed to delete file `{}`; invalid hash size.", &key);
false
}
} else {
warn!("Failed to delete file `{}`; not a md5hash.", &key);
false
}
}
_ => {
warn!("Failed to delete file `{}` from cache: {}", &key, e);
false
}
}
} else {
true
}
}
#[instrument(level = "debug", skip(transaction))]
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,
);
config.proxy.clone().map(|socket| {
if let Some(socket) = config.proxy.clone() {
USE_PROXY
.set(socket)
.expect("USE_PROXY to be set only by this function");
});
}
DISABLE_CERT_VALIDATION.store(
config

View file

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