Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refresh A and AAAA records of active .browse queriers #240

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 67 additions & 3 deletions src/dns_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ impl DnsCache {
pub(crate) fn evict_expired_addr(&mut self, now: u64) -> HashMap<String, HashSet<IpAddr>> {
let mut removed = HashMap::new();

for records in self.addr.values_mut() {
self.addr.retain(|_, records| {
records.retain(|addr| {
let expired = addr.get_record().is_expired(now);
if expired {
Expand All @@ -222,8 +222,10 @@ impl DnsCache {
}
}
!expired
})
}
});

!records.is_empty()
});

removed
}
Expand Down Expand Up @@ -348,6 +350,68 @@ impl DnsCache {
(refresh_due, new_timers)
}

/// Returns the set of `host`, where refreshing the A / AAAA records is due
/// for a `ty_domain`.
pub(crate) fn refresh_due_hosts(&mut self, ty_domain: &str) -> (HashSet<String>, HashSet<u64>) {
let now = current_time_millis();

let mut instances = vec![];

// find instance names for ty_domain.
for record in self.ptr.get_mut(ty_domain).into_iter().flatten() {
if record.get_record().is_expired(now) {
continue;
}

if let Some(ptr) = record.any().downcast_ref::<DnsPointer>() {
instances.push(ptr.alias.clone());
}
}

// Collect hostnames we have browsers for by SRV records.
let mut hostnames_browsed = HashSet::new();
for instance in instances {
let hosts: HashSet<String> = self
.srv
.get(&instance)
.into_iter()
.flatten()
.filter_map(|record| {
if let Some(srv) = record.any().downcast_ref::<DnsSrv>() {
Some(srv.host.clone())
} else {
None
}
})
.collect();

hostnames_browsed.extend(hosts);
}
let mut refresh_due = HashSet::new();
let mut new_timers = HashSet::new();
for hostname in hostnames_browsed {
let refresh_timers: HashSet<u64> = self
.addr
.get_mut(&hostname)
.into_iter()
.flatten()
.filter_map(|record| {
if record.get_record_mut().refresh_maybe(now) {
Some(record.get_record().get_refresh_time())
} else {
None
}
})
.collect();

if !refresh_timers.is_empty() {
refresh_due.insert(hostname);
new_timers.extend(refresh_timers);
}
}
(refresh_due, new_timers)
}

/// Returns the set of A/AAAA records that are due for refresh for a `hostname`.
///
/// For these records, their refresh time will be updated so that they will not refresh again.
Expand Down
11 changes: 10 additions & 1 deletion src/service_daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2217,6 +2217,7 @@ impl Zeroconf {
let mut query_ptr_count = 0;
let mut query_srv_count = 0;
let mut new_timers = HashSet::new();
let mut query_addr_count = 0;

for (ty_domain, _sender) in self.service_queriers.iter() {
let refreshed_timers = self.cache.refresh_due_ptr(ty_domain);
Expand All @@ -2230,10 +2231,17 @@ impl Zeroconf {
let (instances, timers) = self.cache.refresh_due_srv(ty_domain);
for instance in instances.iter() {
debug!("sending refresh query for SRV: {}", instance);
self.send_query(instance, TYPE_ANY);
self.send_query(instance, TYPE_SRV);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@keepsimple1 I also incorporated the fix you suggested

query_srv_count += 1;
}
new_timers.extend(timers);
let (hostnames, timers) = self.cache.refresh_due_hosts(ty_domain);
for hostname in hostnames.iter() {
debug!("sending refresh queries for A and AAAA: {}", hostname);
self.send_query_vec(&[(&hostname, TYPE_A), (&hostname, TYPE_AAAA)]);
query_addr_count += 2;
}
new_timers.extend(timers);
}

for timer in new_timers {
Expand All @@ -2242,6 +2250,7 @@ impl Zeroconf {

self.increase_counter(Counter::CacheRefreshPTR, query_ptr_count);
self.increase_counter(Counter::CacheRefreshSRV, query_srv_count);
self.increase_counter(Counter::CacheRefreshAddr, query_addr_count);
}
}

Expand Down
Loading