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. Co-authored-by: strawberry <strawberry@puppygock.gay> Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
parent
1536f08d81
commit
ec7aeb1096
1 changed files with 23 additions and 21 deletions
|
@ -111,15 +111,13 @@ impl Default for RotationHandler {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type DnsOverrides = Box<dyn Fn(&str) -> Option<SocketAddr> + Send + Sync>;
|
|
||||||
|
|
||||||
struct Resolver {
|
struct Resolver {
|
||||||
inner: GaiResolver,
|
inner: GaiResolver,
|
||||||
overrides: DnsOverrides,
|
overrides: Arc<RwLock<TlsNameMap>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Resolver {
|
impl Resolver {
|
||||||
fn new(overrides: DnsOverrides) -> Resolver {
|
fn new(overrides: Arc<RwLock<TlsNameMap>>) -> Self {
|
||||||
Resolver {
|
Resolver {
|
||||||
inner: GaiResolver::new(),
|
inner: GaiResolver::new(),
|
||||||
overrides,
|
overrides,
|
||||||
|
@ -129,16 +127,26 @@ impl Resolver {
|
||||||
|
|
||||||
impl Resolve for Resolver {
|
impl Resolve for Resolver {
|
||||||
fn resolve(&self, name: Name) -> Resolving {
|
fn resolve(&self, name: Name) -> Resolving {
|
||||||
if let Some(addr) = (self.overrides)(name.as_str()) {
|
self.overrides
|
||||||
let once: Box<dyn Iterator<Item = SocketAddr> + Send> = Box::new(iter::once(addr));
|
.read()
|
||||||
return Box::pin(future::ready(Ok(once)));
|
.expect("lock should not be poisoned")
|
||||||
}
|
.get(name.as_str())
|
||||||
let this = &mut self.inner.clone();
|
.and_then(|(override_name, port)| {
|
||||||
Box::pin(HyperService::<Name>::call(this, name).map(|result| {
|
override_name.first().map(|first_name| {
|
||||||
result
|
let x: Box<dyn Iterator<Item = SocketAddr> + Send> =
|
||||||
.map(|addrs| -> Addrs { Box::new(addrs) })
|
Box::new(iter::once(SocketAddr::new(*first_name, *port)));
|
||||||
.map_err(|err| -> Box<dyn StdError + Send + Sync> { Box::new(err) })
|
let x: Resolving = Box::pin(future::ready(Ok(x)));
|
||||||
}))
|
x
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.unwrap_or_else(|| {
|
||||||
|
let this = &mut self.inner.clone();
|
||||||
|
Box::pin(HyperService::<Name>::call(this, name).map(|result| {
|
||||||
|
result
|
||||||
|
.map(|addrs| -> Addrs { Box::new(addrs) })
|
||||||
|
.map_err(|err| -> Box<dyn StdError + Send + Sync> { Box::new(err) })
|
||||||
|
}))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -163,14 +171,8 @@ impl Service<'_> {
|
||||||
.map(|secret| jsonwebtoken::DecodingKey::from_secret(secret.as_bytes()));
|
.map(|secret| jsonwebtoken::DecodingKey::from_secret(secret.as_bytes()));
|
||||||
|
|
||||||
let default_client = reqwest_client_builder(&config)?.build()?;
|
let default_client = reqwest_client_builder(&config)?.build()?;
|
||||||
let name_override = Arc::clone(&tls_name_override);
|
|
||||||
let federation_client = reqwest_client_builder(&config)?
|
let federation_client = reqwest_client_builder(&config)?
|
||||||
.dns_resolver(Arc::new(Resolver::new(Box::new(move |domain| {
|
.dns_resolver(Arc::new(Resolver::new(tls_name_override.clone())))
|
||||||
let read_guard = name_override.read().unwrap();
|
|
||||||
let (override_name, port) = read_guard.get(domain)?;
|
|
||||||
let first_name = override_name.first()?;
|
|
||||||
Some(SocketAddr::new(*first_name, *port))
|
|
||||||
}))))
|
|
||||||
.build()?;
|
.build()?;
|
||||||
|
|
||||||
// Supported and stable room versions
|
// Supported and stable room versions
|
||||||
|
|
Loading…
Add table
Reference in a new issue