expose multiple types related to InnerConnector

This commit is contained in:
fakeshadow 2021-02-21 13:42:02 -08:00
parent 73c2c68aac
commit 22f251de1a
3 changed files with 86 additions and 106 deletions

View File

@ -187,7 +187,7 @@ impl<T: AsyncRead + AsyncWrite + Unpin> IoConnection<T> {
} }
#[allow(dead_code)] #[allow(dead_code)]
pub(crate) enum EitherIoConnection<A, B> pub enum EitherIoConnection<A, B>
where where
A: AsyncRead + AsyncWrite + Unpin + 'static, A: AsyncRead + AsyncWrite + Unpin + 'static,
B: AsyncRead + AsyncWrite + Unpin + 'static, B: AsyncRead + AsyncWrite + Unpin + 'static,

View File

@ -1,5 +1,8 @@
use std::fmt; use std::fmt;
use std::future::Future;
use std::marker::PhantomData; use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration; use std::time::Duration;
use actix_codec::{AsyncRead, AsyncWrite}; use actix_codec::{AsyncRead, AsyncWrite};
@ -12,7 +15,7 @@ use actix_utils::timeout::{TimeoutError, TimeoutService};
use http::Uri; use http::Uri;
use super::config::ConnectorConfig; use super::config::ConnectorConfig;
use super::connection::Connection; use super::connection::EitherIoConnection;
use super::error::ConnectError; use super::error::ConnectError;
use super::pool::{ConnectionPool, Protocol}; use super::pool::{ConnectionPool, Protocol};
use super::Connect; use super::Connect;
@ -55,7 +58,7 @@ pub struct Connector<T, U> {
_phantom: PhantomData<U>, _phantom: PhantomData<U>,
} }
trait Io: AsyncRead + AsyncWrite + Unpin {} pub trait Io: AsyncRead + AsyncWrite + Unpin {}
impl<T: AsyncRead + AsyncWrite + Unpin> Io for T {} impl<T: AsyncRead + AsyncWrite + Unpin> Io for T {}
impl Connector<(), ()> { impl Connector<(), ()> {
@ -242,8 +245,12 @@ where
/// its combinator chain. /// its combinator chain.
pub fn finish( pub fn finish(
self, self,
) -> impl Service<Connect, Response = impl Connection, Error = ConnectError> + Clone ) -> InnerConnector<
{ impl Service<Connect, Response = (U, Protocol), Error = ConnectError>,
impl Service<Connect, Response = (Box<dyn Io>, Protocol), Error = ConnectError>,
U,
Box<dyn Io>,
> {
let tcp_service = TimeoutService::new( let tcp_service = TimeoutService::new(
self.config.timeout, self.config.timeout,
apply_fn(self.connector.clone(), |msg: Connect, srv| { apply_fn(self.connector.clone(), |msg: Connect, srv| {
@ -259,22 +266,7 @@ where
#[cfg(not(any(feature = "openssl", feature = "rustls")))] #[cfg(not(any(feature = "openssl", feature = "rustls")))]
{ {
use futures_core::future::LocalBoxFuture; InnerConnector {
// A dummy service for annotate tls pool's type signature.
type DummyService = Box<
dyn Service<
Connect,
Response = (Box<dyn Io>, Protocol),
Error = ConnectError,
Future = LocalBoxFuture<
'static,
Result<(Box<dyn Io>, Protocol), ConnectError>,
>,
>,
>;
connect_impl::InnerConnector::<_, DummyService, _, Box<dyn Io>> {
tcp_pool: ConnectionPool::new( tcp_pool: ConnectionPool::new(
tcp_service, tcp_service,
self.config.no_disconnect_timeout(), self.config.no_disconnect_timeout(),
@ -282,6 +274,7 @@ where
tls_pool: None, tls_pool: None,
} }
} }
#[cfg(any(feature = "openssl", feature = "rustls"))] #[cfg(any(feature = "openssl", feature = "rustls"))]
{ {
const H2: &[u8] = b"h2"; const H2: &[u8] = b"h2";
@ -344,7 +337,7 @@ where
TimeoutError::Timeout => ConnectError::Timeout, TimeoutError::Timeout => ConnectError::Timeout,
}); });
connect_impl::InnerConnector { InnerConnector {
tcp_pool: ConnectionPool::new( tcp_pool: ConnectionPool::new(
tcp_service, tcp_service,
self.config.no_disconnect_timeout(), self.config.no_disconnect_timeout(),
@ -355,23 +348,15 @@ where
} }
} }
mod connect_impl { pub struct InnerConnector<S1, S2, Io1, Io2>
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};
use super::*;
use crate::client::connection::EitherIoConnection;
pub(crate) struct InnerConnector<S1, S2, Io1, Io2>
where where
S1: Service<Connect, Response = (Io1, Protocol), Error = ConnectError> + 'static, S1: Service<Connect, Response = (Io1, Protocol), Error = ConnectError> + 'static,
S2: Service<Connect, Response = (Io2, Protocol), Error = ConnectError> + 'static, S2: Service<Connect, Response = (Io2, Protocol), Error = ConnectError> + 'static,
Io1: AsyncRead + AsyncWrite + Unpin + 'static, Io1: AsyncRead + AsyncWrite + Unpin + 'static,
Io2: AsyncRead + AsyncWrite + Unpin + 'static, Io2: AsyncRead + AsyncWrite + Unpin + 'static,
{ {
pub(crate) tcp_pool: ConnectionPool<S1, Io1>, tcp_pool: ConnectionPool<S1, Io1>,
pub(crate) tls_pool: Option<ConnectionPool<S2, Io2>>, tls_pool: Option<ConnectionPool<S2, Io2>>,
} }
impl<S1, S2, Io1, Io2> Clone for InnerConnector<S1, S2, Io1, Io2> impl<S1, S2, Io1, Io2> Clone for InnerConnector<S1, S2, Io1, Io2>
@ -416,7 +401,7 @@ mod connect_impl {
} }
#[pin_project::pin_project(project = InnerConnectorProj)] #[pin_project::pin_project(project = InnerConnectorProj)]
pub(crate) enum InnerConnectorResponse<S1, S2, Io1, Io2> pub enum InnerConnectorResponse<S1, S2, Io1, Io2>
where where
S1: Service<Connect, Response = (Io1, Protocol), Error = ConnectError> + 'static, S1: Service<Connect, Response = (Io1, Protocol), Error = ConnectError> + 'static,
S2: Service<Connect, Response = (Io2, Protocol), Error = ConnectError> + 'static, S2: Service<Connect, Response = (Io2, Protocol), Error = ConnectError> + 'static,
@ -439,19 +424,14 @@ mod connect_impl {
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.project() { match self.project() {
InnerConnectorProj::Io1(fut) => { InnerConnectorProj::Io1(fut) => fut.poll(cx).map_ok(EitherIoConnection::A),
fut.poll(cx).map_ok(EitherIoConnection::A) InnerConnectorProj::Io2(fut) => fut.poll(cx).map_ok(EitherIoConnection::B),
}
InnerConnectorProj::Io2(fut) => {
fut.poll(cx).map_ok(EitherIoConnection::B)
}
InnerConnectorProj::SslIsNotSupported => { InnerConnectorProj::SslIsNotSupported => {
Poll::Ready(Err(ConnectError::SslIsNotSupported)) Poll::Ready(Err(ConnectError::SslIsNotSupported))
} }
} }
} }
} }
}
#[cfg(not(feature = "trust-dns"))] #[cfg(not(feature = "trust-dns"))]
mod resolver { mod resolver {

View File

@ -45,7 +45,7 @@ impl From<Authority> for Key {
} }
/// Connections pool for reuse Io type for certain [`http::uri::Authority`] as key. /// Connections pool for reuse Io type for certain [`http::uri::Authority`] as key.
pub(crate) struct ConnectionPool<S, Io> pub struct ConnectionPool<S, Io>
where where
Io: AsyncWrite + Unpin + 'static, Io: AsyncWrite + Unpin + 'static,
{ {