2024-07-16 23:38:48 +00:00
|
|
|
pub mod actual;
|
|
|
|
|
pub mod cache;
|
|
|
|
|
mod dns;
|
|
|
|
|
pub mod fed;
|
|
|
|
|
mod tests;
|
2024-07-16 22:00:54 +00:00
|
|
|
|
2024-07-16 23:38:48 +00:00
|
|
|
use std::{fmt::Write, sync::Arc};
|
|
|
|
|
|
2025-01-01 23:49:08 +00:00
|
|
|
use conduwuit::{utils, utils::math::Expected, Result, Server};
|
2024-07-16 22:00:54 +00:00
|
|
|
|
2024-07-16 23:38:48 +00:00
|
|
|
use self::{cache::Cache, dns::Resolver};
|
2024-12-03 07:35:48 +00:00
|
|
|
use crate::{client, Dep};
|
2024-07-16 22:00:54 +00:00
|
|
|
|
|
|
|
|
pub struct Service {
|
2024-07-16 23:38:48 +00:00
|
|
|
pub cache: Arc<Cache>,
|
|
|
|
|
pub resolver: Arc<Resolver>,
|
2024-07-18 06:37:47 +00:00
|
|
|
services: Services,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Services {
|
|
|
|
|
server: Arc<Server>,
|
|
|
|
|
client: Dep<client::Service>,
|
2024-07-16 22:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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>> {
|
2024-07-16 23:38:48 +00:00
|
|
|
let cache = Cache::new();
|
2024-07-16 22:00:54 +00:00
|
|
|
Ok(Arc::new(Self {
|
2024-07-16 23:38:48 +00:00
|
|
|
cache: cache.clone(),
|
|
|
|
|
resolver: Resolver::build(args.server, cache)?,
|
2024-07-18 06:37:47 +00:00
|
|
|
services: Services {
|
|
|
|
|
server: args.server.clone(),
|
|
|
|
|
client: args.depend::<client::Service>("client"),
|
|
|
|
|
},
|
2024-07-16 22:00:54 +00:00
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-01 23:49:08 +00:00
|
|
|
fn memory_usage(&self, out: &mut dyn Write) -> Result {
|
|
|
|
|
use utils::bytes::pretty;
|
2024-07-16 22:00:54 +00:00
|
|
|
|
2025-01-01 23:49:08 +00:00
|
|
|
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))?;
|
2024-07-16 22:00:54 +00:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn clear_cache(&self) {
|
2024-07-16 23:38:48 +00:00
|
|
|
self.cache.overrides.write().expect("write locked").clear();
|
|
|
|
|
self.cache
|
|
|
|
|
.destinations
|
|
|
|
|
.write()
|
|
|
|
|
.expect("write locked")
|
|
|
|
|
.clear();
|
|
|
|
|
self.resolver.resolver.clear_cache();
|
2024-07-16 22:00:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn name(&self) -> &str { crate::service::make_name(std::module_path!()) }
|
|
|
|
|
}
|