feat(client,server): remove tcp feature and code (#2929)
This removes the `tcp` feature from hyper's `Cargo.toml`, and the code it enabled: - `HttpConnector` - `GaiResolver` - `AddrStream` And parts of `Client` and `Server` that used those types. Alternatives will be available in the `hyper-util` crate. Closes #2856 Co-authored-by: MrGunflame <mrgunflame@protonmail.com>
This commit is contained in:
@@ -22,18 +22,9 @@
|
||||
//! });
|
||||
//! ```
|
||||
use std::error::Error;
|
||||
use std::future::Future;
|
||||
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6, ToSocketAddrs};
|
||||
use std::pin::Pin;
|
||||
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
|
||||
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) use self::sealed::Resolve;
|
||||
use std::{fmt, vec};
|
||||
|
||||
/// A domain name to resolve into IP addresses.
|
||||
#[derive(Clone, Hash, Eq, PartialEq)]
|
||||
@@ -52,11 +43,6 @@ pub struct GaiAddrs {
|
||||
inner: SocketAddrs,
|
||||
}
|
||||
|
||||
/// A future to resolve a name returned by `GaiResolver`.
|
||||
pub struct GaiFuture {
|
||||
inner: JoinHandle<Result<SocketAddrs, io::Error>>,
|
||||
}
|
||||
|
||||
impl Name {
|
||||
pub(super) fn new(host: Box<str>) -> Name {
|
||||
Name { host }
|
||||
@@ -108,63 +94,12 @@ impl GaiResolver {
|
||||
}
|
||||
}
|
||||
|
||||
impl Service<Name> for GaiResolver {
|
||||
type Response = GaiAddrs;
|
||||
type Error = io::Error;
|
||||
type Future = GaiFuture;
|
||||
|
||||
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 blocking = tokio::task::spawn_blocking(move || {
|
||||
debug!("resolving host={:?}", name.host);
|
||||
(&*name.host, 0)
|
||||
.to_socket_addrs()
|
||||
.map(|i| SocketAddrs { iter: i })
|
||||
});
|
||||
|
||||
GaiFuture { inner: blocking }
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for GaiResolver {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.pad("GaiResolver")
|
||||
}
|
||||
}
|
||||
|
||||
impl Future for GaiFuture {
|
||||
type Output = Result<GaiAddrs, 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(GaiAddrs { inner: addrs }),
|
||||
Ok(Err(err)) => Err(err),
|
||||
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 fmt::Debug for GaiFuture {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.pad("GaiFuture")
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for GaiFuture {
|
||||
fn drop(&mut self) {
|
||||
self.inner.abort();
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for GaiAddrs {
|
||||
type Item = SocketAddr;
|
||||
|
||||
@@ -190,22 +125,6 @@ impl SocketAddrs {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn try_parse(host: &str, port: u16) -> Option<SocketAddrs> {
|
||||
if let Ok(addr) = host.parse::<Ipv4Addr>() {
|
||||
let addr = SocketAddrV4::new(addr, port);
|
||||
return Some(SocketAddrs {
|
||||
iter: vec![SocketAddr::V4(addr)].into_iter(),
|
||||
});
|
||||
}
|
||||
if let Ok(addr) = host.parse::<Ipv6Addr>() {
|
||||
let addr = SocketAddrV6::new(addr, port, 0, 0);
|
||||
return Some(SocketAddrs {
|
||||
iter: vec![SocketAddr::V6(addr)].into_iter(),
|
||||
});
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn filter(self, predicate: impl FnMut(&SocketAddr) -> bool) -> SocketAddrs {
|
||||
SocketAddrs::new(self.iter.filter(predicate).collect())
|
||||
@@ -239,10 +158,6 @@ impl SocketAddrs {
|
||||
pub(super) fn is_empty(&self) -> bool {
|
||||
self.iter.as_slice().is_empty()
|
||||
}
|
||||
|
||||
pub(super) fn len(&self) -> usize {
|
||||
self.iter.as_slice().len()
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for SocketAddrs {
|
||||
@@ -318,12 +233,12 @@ impl Future for TokioThreadpoolGaiFuture {
|
||||
*/
|
||||
|
||||
mod sealed {
|
||||
use super::{SocketAddr, Name};
|
||||
use super::{Name, SocketAddr};
|
||||
use crate::common::{task, Future, Poll};
|
||||
use tower_service::Service;
|
||||
|
||||
// "Trait alias" for `Service<Name, Response = Addrs>`
|
||||
pub trait Resolve {
|
||||
pub(crate) trait 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>>;
|
||||
@@ -352,14 +267,6 @@ mod sealed {
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) async fn resolve<R>(resolver: &mut R, name: Name) -> Result<R::Addrs, R::Error>
|
||||
where
|
||||
R: Resolve,
|
||||
{
|
||||
futures_util::future::poll_fn(|cx| resolver.poll_ready(cx)).await?;
|
||||
resolver.resolve(name).await
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user