style: Document & refactor resolver service

good riddance to this thing
This commit is contained in:
timedout committed 2026-06-27 18:36:44 +01:00
1 parent 77dab8fd20
commit 0c9230f35e
2 files changed
+97 -111

No files matched your search

+52 -65
View File
@@ -3,7 +3,7 @@ use std::{net::IpAddr, sync::Arc, time::SystemTime};
use conduwuit::{
Result,
arrayvec::ArrayVec,
at, err, implement,
at, err,
utils::{math::Expected, rand, stream::TryIgnore},
};
use database::{Cbor, Deserialized, Map};
@@ -43,83 +43,70 @@ impl Cache {
overrides: args.db["servername_override"].clone(),
})
}
}
#[implement(Cache)]
pub async fn clear(&self) { join(self.clear_destinations(), self.clear_overrides()).await; }
pub async fn clear(&self) { join(self.clear_destinations(), self.clear_overrides()).await; }
#[implement(Cache)]
pub async fn clear_destinations(&self) { self.destinations.clear().await; }
pub async fn clear_destinations(&self) { self.destinations.clear().await; }
#[implement(Cache)]
pub async fn clear_overrides(&self) { self.overrides.clear().await; }
pub async fn clear_overrides(&self) { self.overrides.clear().await; }
#[implement(Cache)]
pub fn del_destination(&self, name: &ServerName) { self.destinations.remove(name); }
pub fn del_destination(&self, name: &ServerName) { self.destinations.remove(name); }
#[implement(Cache)]
pub fn del_override(&self, name: &ServerName) { self.overrides.remove(name); }
pub fn del_override(&self, name: &ServerName) { self.overrides.remove(name); }
#[implement(Cache)]
pub fn set_destination(&self, name: &ServerName, dest: &CachedDest) {
self.destinations.raw_put(name, Cbor(dest));
}
pub fn set_destination(&self, name: &ServerName, dest: &CachedDest) {
self.destinations.raw_put(name, Cbor(dest));
}
#[implement(Cache)]
pub fn set_override(&self, name: &str, over: &CachedOverride) {
self.overrides.raw_put(name, Cbor(over));
}
pub fn set_override(&self, name: &str, over: &CachedOverride) {
self.overrides.raw_put(name, Cbor(over));
}
#[implement(Cache)]
#[must_use]
pub async fn has_destination(&self, destination: &ServerName) -> bool {
self.get_destination(destination).await.is_ok()
}
#[must_use]
pub async fn has_destination(&self, destination: &ServerName) -> bool {
self.get_destination(destination).await.is_ok()
}
#[implement(Cache)]
#[must_use]
pub async fn has_override(&self, destination: &str) -> bool {
self.get_override(destination)
.await
.iter()
.any(CachedOverride::valid)
}
#[must_use]
pub async fn has_override(&self, destination: &str) -> bool {
self.get_override(destination)
.await
.iter()
.any(CachedOverride::valid)
}
#[implement(Cache)]
pub async fn get_destination(&self, name: &ServerName) -> Result<CachedDest> {
self.destinations
.get(name)
.await
.deserialized::<Cbor<_>>()
.map(at!(0))
.into_iter()
.find(CachedDest::valid)
.ok_or(err!(Request(NotFound("Expired from cache"))))
}
pub async fn get_destination(&self, name: &ServerName) -> Result<CachedDest> {
self.destinations
.get(name)
.await
.deserialized::<Cbor<_>>()
.map(at!(0))
.into_iter()
.find(CachedDest::valid)
.ok_or(err!(Request(NotFound("Expired from cache"))))
}
#[implement(Cache)]
pub async fn get_override(&self, name: &str) -> Result<CachedOverride> {
self.overrides
.get(name)
.await
.deserialized::<Cbor<_>>()
.map(at!(0))
}
pub async fn get_override(&self, name: &str) -> Result<CachedOverride> {
self.overrides
.get(name)
.await
.deserialized::<Cbor<_>>()
.map(at!(0))
}
#[implement(Cache)]
pub fn destinations(&self) -> impl Stream<Item = (OwnedServerName, CachedDest)> + Send + '_ {
self.destinations
.stream()
.ignore_err()
.map(|item: (OwnedServerName, Cbor<_>)| (item.0, item.1.0))
}
pub fn destinations(&self) -> impl Stream<Item = (OwnedServerName, CachedDest)> + Send + '_ {
self.destinations
.stream()
.ignore_err()
.map(|item: (OwnedServerName, Cbor<_>)| (item.0, item.1.0))
}
#[implement(Cache)]
pub fn overrides(&self) -> impl Stream<Item = (OwnedServerName, CachedOverride)> + Send + '_ {
self.overrides
.stream()
.ignore_err()
.map(|item: (OwnedServerName, Cbor<_>)| (item.0, item.1.0))
pub fn overrides(&self) -> impl Stream<Item = (OwnedServerName, CachedOverride)> + Send + '_ {
self.overrides
.stream()
.ignore_err()
.map(|item: (OwnedServerName, Cbor<_>)| (item.0, item.1.0))
}
}
impl CachedDest {
+45 -46
View File
@@ -1,51 +1,50 @@
use conduwuit::{
Result, debug, debug_error, debug_info, implement, trace, utils::response::LimitReadExt,
};
use conduwuit::{Result, debug, debug_error, debug_info, trace, utils::response::LimitReadExt};
use ruma::ServerName;
#[implement(super::Service)]
#[tracing::instrument(name = "well-known", level = "debug", skip(self, dest))]
pub(super) async fn request_well_known(&self, dest: &str) -> Result<Option<String>> {
trace!("Requesting well known for {dest}");
let response = self
.services
.client
.well_known
.get(format!("https://{dest}/.well-known/matrix/server"))
.send()
.await;
impl super::Service {
#[tracing::instrument(name = "well-known", level = "debug", skip(self, dest))]
pub(super) async fn request_well_known(&self, dest: &str) -> Result<Option<String>> {
trace!("Requesting well known for {dest}");
let response = self
.services
.client
.well_known
.get(format!("https://{dest}/.well-known/matrix/server"))
.send()
.await;
trace!("response: {response:?}");
if let Err(e) = &response {
debug!("error: {e:?}");
return Ok(None);
trace!("response: {response:?}");
if let Err(e) = &response {
debug!("error: {e:?}");
return Ok(None);
}
let response = response?;
if !response.status().is_success() {
debug!("response not 2XX");
return Ok(None);
}
let Ok(text) = response.limit_read_text(8192).await else {
debug!("failed to read well-known response (too large or non-text content)");
return Ok(None);
};
trace!("response text: {text:?}");
let body: serde_json::Value = serde_json::from_str(&text).unwrap_or_default();
let m_server = body
.get("m.server")
.unwrap_or(&serde_json::Value::Null)
.as_str()
.unwrap_or_default();
if ServerName::parse(m_server).is_err() {
debug_error!("response content missing or invalid");
return Ok(None);
}
debug_info!("{dest:?} found at {m_server:?}");
Ok(Some(m_server.to_owned()))
}
let response = response?;
if !response.status().is_success() {
debug!("response not 2XX");
return Ok(None);
}
let Ok(text) = response.limit_read_text(8192).await else {
debug!("failed to read well-known response (too large or non-text content)");
return Ok(None);
};
trace!("response text: {text:?}");
let body: serde_json::Value = serde_json::from_str(&text).unwrap_or_default();
let m_server = body
.get("m.server")
.unwrap_or(&serde_json::Value::Null)
.as_str()
.unwrap_or_default();
if ServerName::parse(m_server).is_err() {
debug_error!("response content missing or invalid");
return Ok(None);
}
debug_info!("{dest:?} found at {m_server:?}");
Ok(Some(m_server.to_owned()))
}