-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
11 changed files
with
145 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
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,116 @@ | ||
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license. | ||
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::GaiResolver; | ||
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, Debug)] | ||
pub enum Resolver { | ||
/// A resolver using blocking `getaddrinfo` calls in a threadpool. | ||
Gai(GaiResolver), | ||
/// trust_dns_resolver userspace resolver. | ||
Trust(AsyncResolver<GenericConnector<TokioRuntimeProvider>>), | ||
} | ||
|
||
impl Default for Resolver { | ||
fn default() -> Self { | ||
Self::gai() | ||
} | ||
} | ||
|
||
impl Resolver { | ||
pub fn gai() -> Self { | ||
Self::Gai(GaiResolver::new()) | ||
} | ||
|
||
/// Create a [`AsyncResolver`] from system conf. | ||
pub fn trust() -> Result<Self, ResolveError> { | ||
Ok(Self::Trust( | ||
trust_dns_resolver::AsyncResolver::tokio_from_system_conf()?, | ||
)) | ||
} | ||
|
||
pub fn trust_from_async_resolver( | ||
resolver: AsyncResolver<GenericConnector<TokioRuntimeProvider>>, | ||
) -> Self { | ||
Self::Trust(resolver) | ||
} | ||
} | ||
|
||
type SocketAddrs = vec::IntoIter<SocketAddr>; | ||
|
||
pub struct ResolveFut { | ||
inner: JoinHandle<Result<SocketAddrs, io::Error>>, | ||
} | ||
|
||
impl Future for ResolveFut { | ||
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), | ||
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 Resolver { | ||
type Response = SocketAddrs; | ||
type Error = io::Error; | ||
type Future = ResolveFut; | ||
|
||
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 task = match self { | ||
Resolver::Gai(gai_resolver) => { | ||
let mut resolver = gai_resolver.clone(); | ||
tokio::spawn(async move { | ||
let result = resolver.call(name).await?; | ||
let x: Vec<_> = result.into_iter().collect(); | ||
let iter: SocketAddrs = x.into_iter(); | ||
Ok(iter) | ||
}) | ||
} | ||
Resolver::Trust(async_resolver) => { | ||
let resolver = async_resolver.clone(); | ||
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) | ||
}) | ||
} | ||
}; | ||
ResolveFut { 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
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
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