//! DNS Resolution used by the `HttpConnector`. //! //! This module contains: //! //! - A [`GaiResolver`](GaiResolver) that is the default resolver for the //! `HttpConnector`. //! - The `Name` type used as an argument to custom resolvers. //! //! # Resolvers are `Service`s //! //! A resolver is just a //! `Service<Name, Response = impl Iterator<Item = SocketAddr>>`. //! //! A simple resolver that ignores the name and always returns a specific //! address: //! //! ```rust,ignore //! use std::{convert::Infallible, iter, net::SocketAddr}; //! //! let resolver = tower::service_fn(|_name| async { //! Ok::<_, Infallible>(iter::once(SocketAddr::from(([127, 0, 0, 1], 8080)))) //! }); //! ``` use std::error::Error; use std::future::Future; use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs}; use std::pin::Pin; use std::str::FromStr; use std::task::{self, Poll}; use std::{fmt, io, vec};
use tokio::task::JoinHandle; use tower_service::Service; use tracing::debug;
pub(super) useself::sealed::Resolve;
/// A domain name to resolve into IP addresses. #[derive(Clone, Hash, Eq, PartialEq)] pubstruct Name {
host: Box<str>,
}
/// A resolver using blocking `getaddrinfo` calls in a threadpool. #[derive(Clone)] pubstruct GaiResolver {
_priv: (),
}
/// An iterator of IP addresses returned from `getaddrinfo`. pubstruct GaiAddrs {
inner: SocketAddrs,
}
/// A future to resolve a name returned by `GaiResolver`. pubstruct GaiFuture {
inner: JoinHandle<Result<SocketAddrs, io::Error>>,
}
impl Name { pub(super) fn new(host: Box<str>) -> Name {
Name { host }
}
/// View the hostname as a string slice. pubfn as_str(&self) -> &str {
&self.host
}
}
impl fmt::Debug for Name { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.host, f)
}
}
impl fmt::Display for Name { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.host, f)
}
}
impl FromStr for Name { type Err = InvalidNameError;
impl Iterator for SocketAddrs { type Item = SocketAddr; #[inline] fn next(&mutself) -> Option<SocketAddr> { self.iter.next()
}
}
/* /// A resolver using `getaddrinfo` calls via the `tokio_executor::threadpool::blocking` API. /// /// Unlike the `GaiResolver` this will not spawn dedicated threads, but only works when running on the /// multi-threaded Tokio runtime. #[cfg(feature="runtime")] #[derive(Clone,Debug)] pubstructTokioThreadpoolGaiResolver(());
/// The future returned by `TokioThreadpoolGaiResolver`. #[cfg(feature="runtime")] #[derive(Debug)] pubstructTokioThreadpoolGaiFuture{ name:Name, }
#[cfg(feature="runtime")] implTokioThreadpoolGaiResolver{ /// Creates a new DNS resolver that will use tokio threadpool's blocking /// feature. /// /// **Requires** its futures to be run on the threadpool runtime. pubfnnew()->Self{ TokioThreadpoolGaiResolver(()) } }
fnpoll(self:Pin<&mutSelf>,_cx:&muttask::Context<'_>)->Poll<Self::Output>{ matchready!(tokio_executor::threadpool::blocking(||( self.name.as_str(), 0 ) .to_socket_addrs())) { Ok(Ok(iter))=>Poll::Ready(Ok(GaiAddrs{ inner:IpAddrs{iter}, })), Ok(Err(e))=>Poll::Ready(Err(e)), // a BlockingError, meaning not on a tokio_executor::threadpool :( Err(e)=>Poll::Ready(Err(io::Error::new(io::ErrorKind::Other,e))), } } }
*/
mod sealed { usesuper::{SocketAddr, Name}; usecrate::common::{task, Future, Poll}; use tower_service::Service;
// "Trait alias" for `Service<Name, Response = Addrs>` pubtrait Resolve { type Addrs: Iterator<Item = SocketAddr>; type Error: Into<Box<dyn std::error::Error + Send + Sync>>; type Future: Future<Output = Result<Self::Addrs, Self::Error>>;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.