implement clear_cache() for resolver service

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2025-03-15 04:08:57 +00:00
parent 9ce95a7030
commit 8010505853
3 changed files with 27 additions and 1 deletions

View file

@ -7,7 +7,7 @@ use conduwuit::{
utils::{math::Expected, rand, stream::TryIgnore}, utils::{math::Expected, rand, stream::TryIgnore},
}; };
use database::{Cbor, Deserialized, Map}; use database::{Cbor, Deserialized, Map};
use futures::{Stream, StreamExt}; use futures::{Stream, StreamExt, future::join};
use ruma::ServerName; use ruma::ServerName;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -45,6 +45,21 @@ impl Cache {
} }
} }
#[implement(Cache)]
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; }
#[implement(Cache)]
pub async fn clear_overrides(&self) { self.overrides.clear().await; }
#[implement(Cache)]
pub fn del_destination(&self, name: &ServerName) { self.destinations.remove(name); }
#[implement(Cache)]
pub fn del_override(&self, name: &ServerName) { self.overrides.remove(name); }
#[implement(Cache)] #[implement(Cache)]
pub fn set_destination(&self, name: &ServerName, dest: &CachedDest) { pub fn set_destination(&self, name: &ServerName, dest: &CachedDest) {
self.destinations.raw_put(name, Cbor(dest)); self.destinations.raw_put(name, Cbor(dest));

View file

@ -78,6 +78,10 @@ impl Resolver {
server: server.clone(), server: server.clone(),
})) }))
} }
/// Clear the in-memory hickory-dns caches
#[inline]
pub fn clear_cache(&self) { self.resolver.clear_cache(); }
} }
impl Resolve for Resolver { impl Resolve for Resolver {

View file

@ -6,6 +6,7 @@ mod tests;
use std::sync::Arc; use std::sync::Arc;
use async_trait::async_trait;
use conduwuit::{Result, Server, arrayvec::ArrayString, utils::MutexMap}; use conduwuit::{Result, Server, arrayvec::ArrayString, utils::MutexMap};
use self::{cache::Cache, dns::Resolver}; use self::{cache::Cache, dns::Resolver};
@ -26,6 +27,7 @@ struct Services {
type Resolving = MutexMap<NameBuf, ()>; type Resolving = MutexMap<NameBuf, ()>;
type NameBuf = ArrayString<256>; type NameBuf = ArrayString<256>;
#[async_trait]
impl crate::Service for Service { impl crate::Service for Service {
#[allow(clippy::as_conversions, clippy::cast_sign_loss, clippy::cast_possible_truncation)] #[allow(clippy::as_conversions, clippy::cast_sign_loss, clippy::cast_possible_truncation)]
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> { fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
@ -41,5 +43,10 @@ impl crate::Service for Service {
})) }))
} }
async fn clear_cache(&self) {
self.resolver.clear_cache();
self.cache.clear().await;
}
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) } fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
} }