use database for resolver caches

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2025-01-22 02:16:51 +00:00
parent 0c96891008
commit 49023aa295
8 changed files with 114 additions and 162 deletions

View file

@ -4,9 +4,9 @@ mod dns;
pub mod fed;
mod tests;
use std::{fmt::Write, sync::Arc};
use std::sync::Arc;
use conduwuit::{utils, utils::math::Expected, Result, Server};
use conduwuit::{Result, Server};
use self::{cache::Cache, dns::Resolver};
use crate::{client, Dep};
@ -25,7 +25,7 @@ struct Services {
impl crate::Service for Service {
#[allow(clippy::as_conversions, clippy::cast_sign_loss, clippy::cast_possible_truncation)]
fn build(args: crate::Args<'_>) -> Result<Arc<Self>> {
let cache = Cache::new();
let cache = Cache::new(&args);
Ok(Arc::new(Self {
cache: cache.clone(),
resolver: Resolver::build(args.server, cache)?,
@ -36,38 +36,5 @@ impl crate::Service for Service {
}))
}
fn memory_usage(&self, out: &mut dyn Write) -> Result {
use utils::bytes::pretty;
let (oc_count, oc_bytes) = self.cache.overrides.read()?.iter().fold(
(0_usize, 0_usize),
|(count, bytes), (key, val)| {
(count.expected_add(1), bytes.expected_add(key.len()).expected_add(val.size()))
},
);
let (dc_count, dc_bytes) = self.cache.destinations.read()?.iter().fold(
(0_usize, 0_usize),
|(count, bytes), (key, val)| {
(count.expected_add(1), bytes.expected_add(key.len()).expected_add(val.size()))
},
);
writeln!(out, "resolver_overrides_cache: {oc_count} ({})", pretty(oc_bytes))?;
writeln!(out, "resolver_destinations_cache: {dc_count} ({})", pretty(dc_bytes))?;
Ok(())
}
fn clear_cache(&self) {
self.cache.overrides.write().expect("write locked").clear();
self.cache
.destinations
.write()
.expect("write locked")
.clear();
self.resolver.resolver.clear_cache();
}
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
}