Skip to content

Commit bcf9ba8

Browse files
committed
fix maxmind integration
1 parent 84dae2e commit bcf9ba8

1 file changed

Lines changed: 14 additions & 19 deletions

File tree

leader-stream/src/geo.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::time::Duration;
1010
use anyhow::{anyhow, Context, Result};
1111
use flate2::read::GzDecoder;
1212
use maxminddb::geoip2::City;
13-
use maxminddb::{MaxMindDBError, Reader};
13+
use maxminddb::{MaxMindDbError, Reader};
1414
use reqwest::Client;
1515
use tar::Archive;
1616
use tokio::sync::RwLock;
@@ -79,12 +79,17 @@ impl GeoIpService {
7979
}
8080
};
8181

82-
let result = match reader.lookup::<City>(ip_addr) {
83-
Ok(city) => extract_point(&city),
84-
Err(err) => {
85-
if !matches!(err, MaxMindDBError::AddressNotFoundError(_)) {
82+
let result = match reader.lookup(ip_addr) {
83+
Ok(lookup) => match lookup.decode::<City>() {
84+
Ok(Some(city)) => extract_point(&city),
85+
Ok(None) => None,
86+
Err(err) => {
8687
self.log_lookup_error_once(err);
88+
None
8789
}
90+
},
91+
Err(err) => {
92+
self.log_lookup_error_once(err);
8893
None
8994
}
9095
};
@@ -97,7 +102,7 @@ impl GeoIpService {
97102
cache.insert(ip.to_string(), value);
98103
}
99104

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

259264
fn extract_point(city: &City) -> Option<GeoPoint> {
260-
let location = city.location.as_ref()?;
265+
let location = &city.location;
261266
let latitude = location.latitude?;
262267
let longitude = location.longitude?;
263-
let city_name = city
264-
.city
265-
.as_ref()
266-
.and_then(|record| record.names.as_ref())
267-
.and_then(|names| names.get("en"))
268-
.map(|value| value.to_string());
269-
let country_name = city
270-
.country
271-
.as_ref()
272-
.and_then(|record| record.names.as_ref())
273-
.and_then(|names| names.get("en"))
274-
.map(|value| value.to_string());
268+
let city_name = city.city.names.english.map(|value| value.to_string());
269+
let country_name = city.country.names.english.map(|value| value.to_string());
275270
Some(GeoPoint {
276271
latitude,
277272
longitude,

0 commit comments

Comments
 (0)