feat(client): change connectors to return an impl Connection
Instead of returning a tuple `(impl AsyncRead + AsyncWrite, Connected)`, this adds a new trait, `hyper::client::connect::Connection`, which allows querying the connection type for a `Connected`. BREAKING CHANGE: Connectors no longer return a tuple of `(T, Connected)`, but a single `T: Connection`.
This commit is contained in:
@@ -17,7 +17,7 @@ use tokio::net::TcpStream;
|
||||
use tokio::time::Delay;
|
||||
|
||||
use super::dns::{self, resolve, GaiResolver, Resolve};
|
||||
use super::{Connected};
|
||||
use super::{Connected, Connection};
|
||||
//#[cfg(feature = "runtime")] use super::dns::TokioThreadpoolGaiResolver;
|
||||
|
||||
|
||||
@@ -234,7 +234,7 @@ where
|
||||
R: Resolve + Clone + Send + Sync + 'static,
|
||||
R::Future: Send,
|
||||
{
|
||||
type Response = (TcpStream, Connected);
|
||||
type Response = TcpStream;
|
||||
type Error = ConnectError;
|
||||
type Future = HttpConnecting<R>;
|
||||
|
||||
@@ -259,7 +259,7 @@ where
|
||||
async fn call_async(
|
||||
&mut self,
|
||||
dst: Uri,
|
||||
) -> Result<(TcpStream, Connected), ConnectError> {
|
||||
) -> Result<TcpStream, ConnectError> {
|
||||
trace!(
|
||||
"Http::connect; scheme={:?}, host={:?}, port={:?}",
|
||||
dst.scheme(),
|
||||
@@ -340,14 +340,20 @@ where
|
||||
sock.set_nodelay(config.nodelay)
|
||||
.map_err(ConnectError::m("tcp set_nodelay error"))?;
|
||||
|
||||
let extra = HttpInfo {
|
||||
remote_addr: sock
|
||||
.peer_addr()
|
||||
.map_err(ConnectError::m("tcp peer_addr error"))?,
|
||||
};
|
||||
let connected = Connected::new().extra(extra);
|
||||
Ok(sock)
|
||||
}
|
||||
}
|
||||
|
||||
Ok((sock, connected))
|
||||
impl Connection for TcpStream {
|
||||
fn connected(&self) -> Connected {
|
||||
let connected = Connected::new();
|
||||
if let Ok(remote_addr) = self.peer_addr() {
|
||||
connected.extra(HttpInfo {
|
||||
remote_addr,
|
||||
})
|
||||
} else {
|
||||
connected
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -372,7 +378,7 @@ pub struct HttpConnecting<R> {
|
||||
_marker: PhantomData<R>,
|
||||
}
|
||||
|
||||
type ConnectResult = Result<(TcpStream, Connected), ConnectError>;
|
||||
type ConnectResult = Result<TcpStream, ConnectError>;
|
||||
type BoxConnecting = Pin<Box<dyn Future<Output = ConnectResult> + Send>>;
|
||||
|
||||
impl<R: Resolve> Future for HttpConnecting<R> {
|
||||
@@ -644,12 +650,12 @@ mod tests {
|
||||
use ::http::Uri;
|
||||
|
||||
use super::super::sealed::Connect;
|
||||
use super::{Connected, HttpConnector};
|
||||
use super::HttpConnector;
|
||||
|
||||
async fn connect<C>(
|
||||
connector: C,
|
||||
dst: Uri,
|
||||
) -> Result<(C::Transport, Connected), C::Error>
|
||||
) -> Result<C::Transport, C::Error>
|
||||
where
|
||||
C: Connect,
|
||||
{
|
||||
|
||||
@@ -13,6 +13,12 @@ use ::http::{Response};
|
||||
#[cfg(feature = "tcp")] mod http;
|
||||
#[cfg(feature = "tcp")] pub use self::http::{HttpConnector, HttpInfo};
|
||||
|
||||
/// Describes a type returned by a connector.
|
||||
pub trait Connection {
|
||||
/// Return metadata describing the connection.
|
||||
fn connected(&self) -> Connected;
|
||||
}
|
||||
|
||||
/// Extra information about the connected transport.
|
||||
///
|
||||
/// This can be used to inform recipients about things like if ALPN
|
||||
@@ -167,7 +173,7 @@ pub(super) mod sealed {
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
|
||||
use crate::common::{Future, Unpin};
|
||||
use super::{Connected};
|
||||
use super::{Connection};
|
||||
|
||||
/// Connect to a destination, returning an IO transport.
|
||||
///
|
||||
@@ -183,21 +189,21 @@ pub(super) mod sealed {
|
||||
// fit the `Connect` bounds because of the blanket impl for `Service`.
|
||||
pub trait Connect: Sealed + Sized {
|
||||
/// The connected IO Stream.
|
||||
type Transport: AsyncRead + AsyncWrite;
|
||||
type Transport: AsyncRead + AsyncWrite + Connection;
|
||||
/// An error occured when trying to connect.
|
||||
type Error: Into<Box<dyn StdError + Send + Sync>>;
|
||||
/// A Future that will resolve to the connected Transport.
|
||||
type Future: Future<Output=Result<(Self::Transport, Connected), Self::Error>>;
|
||||
type Future: Future<Output=Result<Self::Transport, Self::Error>>;
|
||||
#[doc(hidden)]
|
||||
fn connect(self, internal_only: Internal, dst: Uri) -> Self::Future;
|
||||
}
|
||||
|
||||
impl<S, T> Connect for S
|
||||
where
|
||||
S: tower_service::Service<Uri, Response=(T, Connected)> + Send,
|
||||
S: tower_service::Service<Uri, Response=T> + Send,
|
||||
S::Error: Into<Box<dyn StdError + Send + Sync>>,
|
||||
S::Future: Unpin + Send,
|
||||
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
||||
T: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static,
|
||||
{
|
||||
type Transport = T;
|
||||
type Error = S::Error;
|
||||
@@ -209,10 +215,10 @@ pub(super) mod sealed {
|
||||
|
||||
impl<S, T> Sealed for S
|
||||
where
|
||||
S: tower_service::Service<Uri, Response=(T, Connected)> + Send,
|
||||
S: tower_service::Service<Uri, Response=T> + Send,
|
||||
S::Error: Into<Box<dyn StdError + Send + Sync>>,
|
||||
S::Future: Unpin + Send,
|
||||
T: AsyncRead + AsyncWrite + Unpin + Send + 'static,
|
||||
T: AsyncRead + AsyncWrite + Connection + Unpin + Send + 'static,
|
||||
{}
|
||||
|
||||
pub trait Sealed {}
|
||||
|
||||
@@ -71,7 +71,7 @@ use http::uri::Scheme;
|
||||
|
||||
use crate::body::{Body, Payload};
|
||||
use crate::common::{lazy as hyper_lazy, BoxSendFuture, Executor, Lazy, Future, Pin, Poll, task};
|
||||
use self::connect::{Alpn, sealed::Connect, Connected};
|
||||
use self::connect::{Alpn, sealed::Connect, Connected, Connection};
|
||||
use self::pool::{Key as PoolKey, Pool, Poolable, Pooled, Reservation};
|
||||
|
||||
#[cfg(feature = "tcp")] pub use self::connect::HttpConnector;
|
||||
@@ -478,7 +478,8 @@ where C: Connect + Clone + Send + Sync + 'static,
|
||||
};
|
||||
Either::Left(connector.connect(connect::sealed::Internal, dst)
|
||||
.map_err(crate::Error::new_connect)
|
||||
.and_then(move |(io, connected)| {
|
||||
.and_then(move |io| {
|
||||
let connected = io.connected();
|
||||
// If ALPN is h2 and we aren't http2_only already,
|
||||
// then we need to convert our pool checkout into
|
||||
// a single HTTP2 one.
|
||||
|
||||
Reference in New Issue
Block a user