Skip to content

Commit

Permalink
address pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyv1987 committed Oct 26, 2023
1 parent 8139fcf commit 76d4036
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
18 changes: 12 additions & 6 deletions explorer-api/src/geo_ip/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,24 @@ impl GeoIp {
Ok(ip)
} else {
debug!("Fail to resolve IP address from {}:{}", &address, p);
let mut failed_ips_guard = self.failed_addresses.failed_ips.lock().unwrap();
if failed_ips_guard.insert(address.to_string()) {
append_ip_to_file(address);
if let Ok(mut failed_ips_guard) = self.failed_addresses.failed_ips.lock() {
if failed_ips_guard.insert(address.to_string()) {
append_ip_to_file(address);
}
} else {
error!("Failed to acquire lock on failed_ips");
}
Err(GeoIpError::NoValidIP)
}
}
Err(_) => {
debug!("Fail to resolve IP address from {}:{}.", &address, p);
let mut failed_ips_guard = self.failed_addresses.failed_ips.lock().unwrap();
if failed_ips_guard.insert(address.to_string()) {
append_ip_to_file(address);
if let Ok(mut failed_ips_guard) = self.failed_addresses.failed_ips.lock() {
if failed_ips_guard.insert(address.to_string()) {
append_ip_to_file(address);
}
} else {
error!("Failed to acquire lock on failed_ips");
}
Err(GeoIpError::NoValidIP)
}
Expand Down
11 changes: 9 additions & 2 deletions explorer-api/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@ pub fn failed_ips_filepath() -> String {
}

pub fn append_ip_to_file(address: &str) {
if let Ok(mut file) = OpenOptions::new()
match OpenOptions::new()
.append(true)
.create(true)
.open(failed_ips_filepath())
{
writeln!(file, "{}", address).expect("Failed to write to file");
Ok(mut file) => {
if let Err(e) = writeln!(file, "{}", address) {
error!("Failed to write to file: {}", e);
}
}
Err(e) => {
error!("Failed to open or create the file: {}", e);
}
}
}

0 comments on commit 76d4036

Please sign in to comment.