-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(ext/fetch): use
trust_dns_resolver
instead of default `GaiReso…
…lver`
- Loading branch information
Showing
6 changed files
with
94 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
use std::future::Future; | ||
use std::io; | ||
use std::net::SocketAddr; | ||
use std::pin::Pin; | ||
use std::task::Poll; | ||
use std::task::{self}; | ||
use std::vec; | ||
|
||
use hyper_util::client::legacy::connect::dns::Name; | ||
use tokio::task::JoinHandle; | ||
use tower::Service; | ||
use trust_dns_resolver::error::ResolveError; | ||
use trust_dns_resolver::name_server::GenericConnector; | ||
use trust_dns_resolver::name_server::TokioRuntimeProvider; | ||
use trust_dns_resolver::AsyncResolver; | ||
|
||
#[derive(Clone)] | ||
pub struct TrustResolver { | ||
resolver: AsyncResolver<GenericConnector<TokioRuntimeProvider>>, | ||
} | ||
|
||
impl TrustResolver { | ||
pub fn new() -> Result<TrustResolver, ResolveError> { | ||
Ok(Self { | ||
resolver: trust_dns_resolver::AsyncResolver::tokio_from_system_conf()?, | ||
}) | ||
} | ||
} | ||
|
||
type SocketAddrs = vec::IntoIter<SocketAddr>; | ||
|
||
pub struct AltResolverFut { | ||
inner: JoinHandle<Result<SocketAddrs, ResolveError>>, | ||
} | ||
|
||
impl Future for AltResolverFut { | ||
type Output = Result<SocketAddrs, io::Error>; | ||
|
||
fn poll( | ||
mut self: Pin<&mut Self>, | ||
cx: &mut task::Context<'_>, | ||
) -> Poll<Self::Output> { | ||
Pin::new(&mut self.inner).poll(cx).map(|res| match res { | ||
Ok(Ok(addrs)) => Ok(addrs), | ||
Ok(Err(e)) => Err(e.into()), | ||
Err(join_err) => { | ||
if join_err.is_cancelled() { | ||
Err(io::Error::new(io::ErrorKind::Interrupted, join_err)) | ||
} else { | ||
panic!("gai background task failed: {:?}", join_err) | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
impl Service<Name> for TrustResolver { | ||
type Response = SocketAddrs; | ||
type Error = io::Error; | ||
type Future = AltResolverFut; | ||
|
||
fn poll_ready( | ||
&mut self, | ||
_cx: &mut task::Context<'_>, | ||
) -> Poll<Result<(), io::Error>> { | ||
Poll::Ready(Ok(())) | ||
} | ||
|
||
fn call(&mut self, name: Name) -> Self::Future { | ||
let resolver = self.resolver.clone(); | ||
let task = tokio::spawn(async move { | ||
let result = resolver.lookup_ip(name.as_str()).await?; | ||
|
||
let x: Vec<_> = | ||
result.into_iter().map(|x| SocketAddr::new(x, 0)).collect(); | ||
let iter: SocketAddrs = x.into_iter(); | ||
Ok(iter) | ||
}); | ||
|
||
AltResolverFut { inner: task } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters