Recursor: make nameserver and record cache sizes configurable

This commit is contained in:
Marcus Butler 2023-12-24 10:42:59 -06:00 committed by Benjamin Fry
parent 1d944684bc
commit b414fe8d78
5 changed files with 18 additions and 7 deletions

View File

@ -49,18 +49,19 @@ impl Recursor {
/// # Panics
///
/// This will panic if the roots are empty.
pub fn new(roots: impl Into<NameServerConfigGroup>) -> Result<Self, ResolveError> {
pub fn new(roots: impl Into<NameServerConfigGroup>, ns_cache_size: usize, record_cache_size: usize) -> Result<Self, ResolveError> {
// configure the hickory-resolver
let roots: NameServerConfigGroup = roots.into();
assert!(!roots.is_empty(), "roots must not be empty");
debug!("Using cache sizes {}/{}", ns_cache_size, record_cache_size);
let opts = recursor_opts();
let roots =
GenericNameServerPool::from_config(roots, opts, TokioConnectionProvider::default());
let roots = RecursorPool::from(Name::root(), roots);
let name_server_cache = Mutex::new(NameServerCache::new(100)); // TODO: make this configurable
let record_cache = DnsLru::new(100, TtlConfig::default());
let name_server_cache = Mutex::new(NameServerCache::new(ns_cache_size));
let record_cache = DnsLru::new(record_cache_size, TtlConfig::default());
Ok(Self {
roots,
@ -301,7 +302,6 @@ impl Recursor {
.into_iter()
.chain(r.take_name_servers())
.chain(r.take_additionals());
let lookup = self.record_cache.insert_records(query, records, now);
lookup.ok_or_else(|| Error::from("no records found"))

View File

@ -74,7 +74,7 @@ impl RecursiveAuthority {
}
let recursor =
Recursor::new(roots).map_err(|e| format!("failed to initialize recursor: {e}"))?;
Recursor::new(roots, config.ns_cache_size, config.record_cache_size).map_err(|e| format!("failed to initialize recursor: {e}"))?;
Ok(Self {
origin: origin.into(),

View File

@ -27,6 +27,14 @@ use crate::resolver::Name;
pub struct RecursiveConfig {
/// File with roots, aka hints
pub roots: PathBuf,
/// Maximum nameserver cache size
#[serde(default = "ns_cache_size_default")]
pub ns_cache_size: usize,
/// Maximum DNS record cache size
#[serde(default = "record_cache_size_default")]
pub record_cache_size: usize,
}
impl RecursiveConfig {
@ -57,3 +65,6 @@ impl RecursiveConfig {
.collect())
}
}
fn ns_cache_size_default() -> usize { 1024 }
fn record_cache_size_default() -> usize { 1048576 }

View File

@ -37,4 +37,4 @@ zone_type = "Hint"
## remember the port, defaults: 53 for Udp & Tcp, 853 for Tls and 443 for Https.
## Tls and/or Https require features dns-over-tls and/or dns-over-https
stores = { type = "recursor", roots = "default/root.zone" }
stores = { type = "recursor", roots = "default/root.zone", ns_cache_size = 1024, record_cache_size = 1048576 }

View File

@ -157,7 +157,7 @@ pub async fn main() -> Result<(), Box<dyn std::error::Error>> {
let name = opts.domainname;
let ty = opts.ty;
let recursor = Recursor::new(roots)?;
let recursor = Recursor::new(roots, 1024, 1048576)?;
// execute query
println!(