fix: also cache instances without metadata

This commit is contained in:
əlemi 2023-10-02 14:35:52 +02:00
parent eb32729b15
commit 0a3b4781cc
2 changed files with 13 additions and 6 deletions

View file

@ -11,11 +11,11 @@ const MAX_CACHE_AGE : i64 = 86400;
#[derive(Default)] #[derive(Default)]
pub struct InstanceCache { pub struct InstanceCache {
store: RwLock<HashMap<String, (i64, serde_json::Value)>>, store: RwLock<HashMap<String, (i64, Option<serde_json::Value>)>>,
} }
impl InstanceCache { impl InstanceCache {
pub async fn instance_metadata(&self, domain: &str) -> reqwest::Result<serde_json::Value> { pub async fn instance_metadata(&self, domain: &str) -> reqwest::Result<Option<serde_json::Value>> {
let now = Utc::now().timestamp(); let now = Utc::now().timestamp();
if let Some((age, value)) = self.store.read().await.get(domain) { if let Some((age, value)) = self.store.read().await.get(domain) {
@ -24,15 +24,18 @@ impl InstanceCache {
} }
} }
let value = reqwest::Client::builder() let response = reqwest::Client::builder()
.timeout(Duration::from_secs(5)) .timeout(Duration::from_secs(5))
.build()? .build()?
.get(format!("https://{}/nodeinfo/2.0.json", domain)) .get(format!("https://{}/nodeinfo/2.0.json", domain))
.send() .send()
.await?
.json::<serde_json::Value>()
.await?; .await?;
let value = response
.json::<serde_json::Value>()
.await
.ok();
self.store.write().await.insert(domain.to_string(), (now, value.clone())); self.store.write().await.insert(domain.to_string(), (now, value.clone()));
Ok(value) Ok(value)

View file

@ -68,7 +68,11 @@ async fn scan_instance(domain: &str, map: Arc<Mutex<Map>>) -> Option<()> {
tracing::debug!("scanning instance {}", domain); tracing::debug!("scanning instance {}", domain);
let response = match CACHE.instance_metadata(domain).await { let response = match CACHE.instance_metadata(domain).await {
Ok(r) => r, Ok(Some(r)) => r,
Ok(None) => {
tracing::info!("instance {} doesn't provide metadata", domain);
return None
},
Err(e) => { Err(e) => {
tracing::warn!("could not fetch metadata for {}: {}", domain, e); tracing::warn!("could not fetch metadata for {}: {}", domain, e);
return None return None