Skip to content

Commit

Permalink
fix: remove indirection in connectors:tls:Connector
Browse files Browse the repository at this point in the history
 replace direct field access with getter method that
 returns &Arc<impl TlsConnectorContext>,
 store concrete type in now private struct field

using impl Trait in return value keeps method signature
stable across features; allows for static dispatch

 adjust visibility for TlsConnectorCtx structs from pub(crate) to pub(super)
  • Loading branch information
hargut committed Sep 9, 2024
1 parent 97a3df7 commit bc83020
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
12 changes: 7 additions & 5 deletions pingora-core/src/connectors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,13 @@ impl TransportConnector {
let stream = if let Some(rt) = rt {
let peer = peer.clone();
let tls_ctx = self.tls_ctx.clone();
rt.spawn(async move { do_connect(&peer, bind_to, alpn_override, &tls_ctx.ctx).await })
.await
.or_err(InternalError, "offload runtime failure")??
rt.spawn(
async move { do_connect(&peer, bind_to, alpn_override, tls_ctx.context()).await },
)
.await
.or_err(InternalError, "offload runtime failure")??
} else {
do_connect(peer, bind_to, alpn_override, &self.tls_ctx.ctx).await?
do_connect(peer, bind_to, alpn_override, self.tls_ctx.context()).await?
};

Ok(stream)
Expand Down Expand Up @@ -437,7 +439,7 @@ mod tests {
/// the decomposed error type and message
async fn get_do_connect_failure_with_peer(peer: &BasicPeer) -> (ErrorType, String) {
let connector = Connector::new(None);
let stream = do_connect(peer, None, None, &connector.ctx).await;
let stream = do_connect(peer, None, None, connector.context()).await;
match stream {
Ok(_) => panic!("should throw an error"),
Err(e) => (
Expand Down
7 changes: 4 additions & 3 deletions pingora-core/src/connectors/tls/boringssl_openssl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn init_ssl_cert_env_vars() {
INIT_CA_ENV.call_once(openssl_probe::init_ssl_cert_env_vars);
}

pub(crate) struct TlsConnectorCtx(pub(crate) SslConnector);
pub(super) struct TlsConnectorCtx(SslConnector);

impl TlsConnectorContext for TlsConnectorCtx {
fn as_any(&self) -> &dyn Any {
Expand Down Expand Up @@ -162,15 +162,16 @@ impl TlsConnectorContext for TlsConnectorCtx {
}
}

pub(super) async fn connect<T, P>(
pub(super) async fn connect<T, P, C>(
stream: T,
peer: &P,
alpn_override: Option<ALPN>,
tls_ctx: &Arc<dyn TlsConnectorContext + Send + Sync>,
tls_ctx: &Arc<C>,
) -> Result<TlsStream<T>>
where
T: IO,
P: Peer + Send + Sync,
C: TlsConnectorContext + Send + Sync,
{
let ctx = tls_ctx.as_any().downcast_ref::<TlsConnectorCtx>().unwrap();
let mut ssl_conf = ctx.0.configure().unwrap();
Expand Down
28 changes: 20 additions & 8 deletions pingora-core/src/connectors/tls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,37 @@ pub(crate) mod rustls;

#[derive(Clone)]
pub struct Connector {
pub(crate) ctx: Arc<dyn TlsConnectorContext + Send + Sync>, // Arc to support clone
ctx: Arc<TlsConnectorCtx>, // Arc to support clone
}

impl Connector {
pub fn new(options: Option<ConnectorOptions>) -> Self {
TlsConnectorCtx::build_connector(options)
}

pub fn context(&self) -> &Arc<impl TlsConnectorContext> {
&self.ctx
}
}

pub(crate) trait TlsConnectorContext {
pub trait TlsConnectorContext {
fn as_any(&self) -> &dyn Any;

fn build_connector(options: Option<ConnectorOptions>) -> Connector
where
Self: Sized;
}

pub(super) async fn do_connect<P: Peer + Send + Sync>(
pub(super) async fn do_connect<P, C>(
peer: &P,
bind_to: Option<SocketAddr>,
alpn_override: Option<ALPN>,
tls_ctx: &Arc<dyn TlsConnectorContext + Send + Sync>,
) -> Result<Stream> {
tls_ctx: &Arc<C>,
) -> Result<Stream>
where
P: Peer + Send + Sync,
C: TlsConnectorContext + Send + Sync,
{
// Create the future that does the connections, but don't evaluate it until
// we decide if we need a timeout or not
let connect_future = do_connect_inner(peer, bind_to, alpn_override, tls_ctx);
Expand All @@ -79,12 +87,16 @@ pub(super) async fn do_connect<P: Peer + Send + Sync>(
}
}

async fn do_connect_inner<P: Peer + Send + Sync>(
async fn do_connect_inner<P, C>(
peer: &P,
bind_to: Option<SocketAddr>,
alpn_override: Option<ALPN>,
tls_ctx: &Arc<dyn TlsConnectorContext + Send + Sync>,
) -> Result<Stream> {
tls_ctx: &Arc<C>,
) -> Result<Stream>
where
P: Peer + Send + Sync,
C: TlsConnectorContext + Send + Sync,
{
let stream = l4_connect(peer, bind_to).await?;
if peer.tls() {
let tls_stream = tls_connect(stream, peer, alpn_override, tls_ctx).await?;
Expand Down
7 changes: 4 additions & 3 deletions pingora-core/src/connectors/tls/rustls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use crate::upstreams::peer::Peer;

use super::{replace_leftmost_underscore, Connector, TlsConnectorContext};

pub(crate) struct TlsConnectorCtx {
pub(super) struct TlsConnectorCtx {
config: RusTlsClientConfig,
ca_certs: RootCertStore,
}
Expand Down Expand Up @@ -110,15 +110,16 @@ impl TlsConnectorContext for TlsConnectorCtx {
}
}

pub(super) async fn connect<T, P>(
pub(super) async fn connect<T, P, C>(
stream: T,
peer: &P,
alpn_override: Option<ALPN>,
tls_ctx: &Arc<dyn TlsConnectorContext + Send + Sync>,
tls_ctx: &Arc<C>,
) -> Result<TlsStream<T>>
where
T: IO,
P: Peer + Send + Sync,
C: TlsConnectorContext + Send + Sync,
{
let ctx = tls_ctx.as_any().downcast_ref::<TlsConnectorCtx>().unwrap();
let mut config = ctx.config.clone();
Expand Down

0 comments on commit bc83020

Please sign in to comment.