From fe86d28428a0cae53ac9ca5f5efe7783cb00022c Mon Sep 17 00:00:00 2001 From: Charles Hall Date: Wed, 24 Jan 2024 15:11:17 -0800 Subject: [PATCH] move resolver logic into the resolver Honestly not sure why it wasn't done like this before. This code is much less awkward to follow and more compartmentalized. These changes were mainly motivated by a clippy lint triggering on the original code, which then made me wonder if I could get rid of some of the `Box`ing. Turns out I could, and this is the result of that. --- src/service/globals/mod.rs | 42 +++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/src/service/globals/mod.rs b/src/service/globals/mod.rs index 96b2954f..f034415e 100644 --- a/src/service/globals/mod.rs +++ b/src/service/globals/mod.rs @@ -109,11 +109,11 @@ impl Default for RotationHandler { pub struct Resolver { inner: GaiResolver, - overrides: Box Option + Send + Sync>, + overrides: Arc>, } impl Resolver { - pub fn new(overrides: Box Option + Send + Sync>) -> Resolver { + pub fn new(overrides: Arc>) -> Self { Resolver { inner: GaiResolver::new(), overrides, @@ -123,16 +123,26 @@ impl Resolver { impl Resolve for Resolver { fn resolve(&self, name: Name) -> Resolving { - if let Some(addr) = (self.overrides)(name.as_str()) { - let once: Box + Send> = Box::new(iter::once(addr)); - return Box::pin(future::ready(Ok(once))); - } - let this = &mut self.inner.clone(); - Box::pin(HyperService::::call(this, name).map(|result| { - result - .map(|addrs| -> Addrs { Box::new(addrs) }) - .map_err(|err| -> Box { Box::new(err) }) - })) + self.overrides + .read() + .expect("lock should not be poisoned") + .get(name.as_str()) + .and_then(|(override_name, port)| { + override_name.get(0).map(|first_name| { + let x: Box + Send> = + Box::new(iter::once(SocketAddr::new(*first_name, *port))); + let x: Resolving = Box::pin(future::ready(Ok(x))); + x + }) + }) + .unwrap_or_else(|| { + let this = &mut self.inner.clone(); + Box::pin(HyperService::::call(this, name).map(|result| { + result + .map(|addrs| -> Addrs { Box::new(addrs) }) + .map_err(|err| -> Box { Box::new(err) }) + })) + }) } } @@ -157,14 +167,8 @@ impl Service { .map(|secret| jsonwebtoken::DecodingKey::from_secret(secret.as_bytes())); let default_client = reqwest_client_builder(&config)?.build()?; - let name_override = Arc::clone(&tls_name_override); let federation_client = reqwest_client_builder(&config)? - .dns_resolver(Arc::new(Resolver::new(Box::new(move |domain| { - let read_guard = name_override.read().unwrap(); - let (override_name, port) = read_guard.get(domain)?; - let first_name = override_name.get(0)?; - Some(SocketAddr::new(*first_name, *port)) - })))) + .dns_resolver(Arc::new(Resolver::new(tls_name_override.clone()))) .build()?; // Supported and stable room versions