Mercurial > core / rust/lib/net/src/engine/dns/client.rs
changeset 698: |
96958d3eb5b0 |
parent: |
611060cba9f0
|
author: |
Richard Westhaver <ellis@rwest.io> |
date: |
Fri, 04 Oct 2024 22:04:59 -0400 |
permissions: |
-rw-r--r-- |
description: |
fixes |
7 thread::{Builder, JoinHandle}, 12 sync::mpsc::{self, Sender}, 15 use super::{resolver::Lookup, IpMap}; 17 type PendingAddrs = HashSet<IpAddr>; 19 const CHANNEL_SIZE: usize = 1_000; 22 cache: Arc<Mutex<IpMap>>, 23 pending: Arc<Mutex<PendingAddrs>>, 24 tx: Option<Sender<Vec<IpAddr>>>, 25 handle: Option<JoinHandle<()>>, 29 pub fn new<R>(resolver: R, runtime: Runtime) -> Result<Self> 31 R: Lookup + Send + Sync + 'static, 33 let cache = Arc::new(Mutex::new(IpMap::new())); 34 let pending = Arc::new(Mutex::new(PendingAddrs::new())); 35 let (tx, mut rx) = mpsc::channel::<Vec<IpAddr>>(CHANNEL_SIZE); 37 let handle = Builder::new().name("resolver".into()).spawn({ 38 let cache = cache.clone(); 39 let pending = pending.clone(); 41 runtime.block_on(async { 42 let resolver = Arc::new(resolver); 44 while let Some(ips) = rx.recv().await { 47 let resolver = resolver.clone(); 48 let cache = cache.clone(); 49 let pending = pending.clone(); 51 if let Some(n) = resolver.lookup(ip).await { 52 let name: String = n.to_owned(); 53 cache.lock().unwrap().insert(ip, name); 55 pending.lock().unwrap().remove(&ip); 72 pub fn resolve(&mut self, ips: Vec<IpAddr>) { 73 // Remove ips that are already being resolved 76 .filter(|ip| self.pending.lock().unwrap().insert(*ip)) 80 // Discard the message if the channel is full; it will be retried 82 let _ = self.tx.as_mut().unwrap().try_send(ips); 86 pub fn cache(&mut self) -> IpMap { 87 let cache = self.cache.lock().unwrap(); 92 impl Drop for Client { 94 // Do the Option dance to be able to drop the sender so that the receiver 95 // finishes and the thread can be joined 96 drop(self.tx.take().unwrap()); 97 self.handle.take().unwrap().join().unwrap();