Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 27 additions & 9 deletions leader-stream/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion leader-stream/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ axum = { version = "0.7", features = ["macros"] }
bytes = "1"
flate2 = "1"
futures-util = "0.3"
maxminddb = "0.24"
maxminddb = "0.27"
reqwest = { version = "0.11", features = ["json", "rustls-tls", "stream"] }
rustls = { version = "0.23", default-features = false, features = ["ring"] }
serde_json = "1"
Expand Down
33 changes: 14 additions & 19 deletions leader-stream/src/geo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::time::Duration;
use anyhow::{anyhow, Context, Result};
use flate2::read::GzDecoder;
use maxminddb::geoip2::City;
use maxminddb::{MaxMindDBError, Reader};
use maxminddb::{MaxMindDbError, Reader};
use reqwest::Client;
use tar::Archive;
use tokio::sync::RwLock;
Expand Down Expand Up @@ -79,12 +79,17 @@ impl GeoIpService {
}
};

let result = match reader.lookup::<City>(ip_addr) {
Ok(city) => extract_point(&city),
Err(err) => {
if !matches!(err, MaxMindDBError::AddressNotFoundError(_)) {
let result = match reader.lookup(ip_addr) {
Ok(lookup) => match lookup.decode::<City>() {
Ok(Some(city)) => extract_point(&city),
Ok(None) => None,
Err(err) => {
self.log_lookup_error_once(err);
None
}
},
Err(err) => {
self.log_lookup_error_once(err);
None
}
};
Expand All @@ -97,7 +102,7 @@ impl GeoIpService {
cache.insert(ip.to_string(), value);
}

fn log_lookup_error_once(&self, err: MaxMindDBError) {
fn log_lookup_error_once(&self, err: MaxMindDbError) {
if !self.lookup_error_logged.swap(true, Ordering::SeqCst) {
warn!(
?err,
Expand Down Expand Up @@ -257,21 +262,11 @@ async fn fetch_and_write(client: &Client, url: &str, target: &Path, raw_mmdb: bo
}

fn extract_point(city: &City) -> Option<GeoPoint> {
let location = city.location.as_ref()?;
let location = &city.location;
let latitude = location.latitude?;
let longitude = location.longitude?;
let city_name = city
.city
.as_ref()
.and_then(|record| record.names.as_ref())
.and_then(|names| names.get("en"))
.map(|value| value.to_string());
let country_name = city
.country
.as_ref()
.and_then(|record| record.names.as_ref())
.and_then(|names| names.get("en"))
.map(|value| value.to_string());
let city_name = city.city.names.english.map(|value| value.to_string());
let country_name = city.country.names.english.map(|value| value.to_string());
Some(GeoPoint {
latitude,
longitude,
Expand Down