Skip to content

Commit

Permalink
(imsnif#127) Degrading retry DNS resolve. Needs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bigtoast committed Jan 18, 2020
1 parent 6de8270 commit 669e54b
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/network/dns/client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::network::dns::{resolver::Lookup, IpTable};
use std::{
collections::HashSet,
collections::HashMap,
net::IpAddr,
sync::{Arc, Mutex},
thread::{Builder, JoinHandle},
Expand All @@ -10,13 +10,13 @@ use tokio::{
sync::mpsc::{self, Sender},
};

type PendingAddrs = HashSet<IpAddr>;
type PendingAddrs = HashMap<IpAddr,f32>;


const CHANNEL_SIZE: usize = 1_000;

pub struct Client {
cache: Arc<Mutex<IpTable>>,
pending: Arc<Mutex<PendingAddrs>>,
tx: Option<Sender<Vec<IpAddr>>>,
handle: Option<JoinHandle<()>>,
}
Expand All @@ -38,6 +38,16 @@ impl Client {
let resolver = Arc::new(resolver);

while let Some(ips) = rx.recv().await {
let ips = ips
.into_iter()
.filter(|ip| {
let mut pending = pending.lock().unwrap().clone();
let cnt = pending.entry(*ip).or_insert(0.0);
let pwr_of2 :bool = (*cnt == 0.0) | (cnt.log2() % 1.0 == 0.0);
*cnt += 1.0;
pwr_of2
}).collect::<Vec<_>>();

for ip in ips {
tokio::spawn({
let resolver = resolver.clone();
Expand All @@ -47,8 +57,8 @@ impl Client {
async move {
if let Some(name) = resolver.lookup(ip).await {
cache.lock().unwrap().insert(ip, name);
pending.lock().unwrap().remove(&ip);
}
pending.lock().unwrap().remove(&ip);
}
});
}
Expand All @@ -59,19 +69,12 @@ impl Client {

Ok(Self {
cache,
pending,
tx: Some(tx),
handle: Some(handle),
})
}

pub fn resolve(&mut self, ips: Vec<IpAddr>) {
// Remove ips that are already being resolved
let ips = ips
.into_iter()
.filter(|ip| self.pending.lock().unwrap().insert(ip.clone()))
.collect::<Vec<_>>();

if !ips.is_empty() {
// Discard the message if the channel is full; it will be retried eventually
let _ = self.tx.as_mut().unwrap().try_send(ips);
Expand Down

0 comments on commit 669e54b

Please sign in to comment.