Skip to content

Commit

Permalink
chore: Remove redundant helper constructor (#1534)
Browse files Browse the repository at this point in the history
* chore: Remove redundant helper constructor

* chore: Refactor endpoint to construct connector
  • Loading branch information
tottoto authored Nov 15, 2023
1 parent 7562600 commit 9123f68
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 39 deletions.
35 changes: 14 additions & 21 deletions tonic/src/transport/channel/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,18 +301,24 @@ impl Endpoint {
self
}

pub(crate) fn connector<C>(&self, c: C) -> service::Connector<C> {
#[cfg(feature = "tls")]
let connector = service::Connector::new(c, self.tls.clone());

#[cfg(not(feature = "tls"))]
let connector = service::Connector::new(c);

connector
}

/// Create a channel from this config.
pub async fn connect(&self) -> Result<Channel, Error> {
let mut http = hyper::client::connect::HttpConnector::new();
http.enforce_http(false);
http.set_nodelay(self.tcp_nodelay);
http.set_keepalive(self.tcp_keepalive);

#[cfg(feature = "tls")]
let connector = service::connector(http, self.tls.clone());

#[cfg(not(feature = "tls"))]
let connector = service::connector(http);
let connector = self.connector(http);

if let Some(connect_timeout) = self.connect_timeout {
let mut connector = hyper_timeout::TimeoutConnector::new(connector);
Expand All @@ -333,11 +339,7 @@ impl Endpoint {
http.set_nodelay(self.tcp_nodelay);
http.set_keepalive(self.tcp_keepalive);

#[cfg(feature = "tls")]
let connector = service::connector(http, self.tls.clone());

#[cfg(not(feature = "tls"))]
let connector = service::connector(http);
let connector = self.connector(http);

if let Some(connect_timeout) = self.connect_timeout {
let mut connector = hyper_timeout::TimeoutConnector::new(connector);
Expand All @@ -362,11 +364,7 @@ impl Endpoint {
C::Future: Send + 'static,
crate::Error: From<C::Error> + Send + 'static,
{
#[cfg(feature = "tls")]
let connector = service::connector(connector, self.tls.clone());

#[cfg(not(feature = "tls"))]
let connector = service::connector(connector);
let connector = self.connector(connector);

if let Some(connect_timeout) = self.connect_timeout {
let mut connector = hyper_timeout::TimeoutConnector::new(connector);
Expand All @@ -391,12 +389,7 @@ impl Endpoint {
C::Future: Send + 'static,
crate::Error: From<C::Error> + Send + 'static,
{
#[cfg(feature = "tls")]
let connector = service::connector(connector, self.tls.clone());

#[cfg(not(feature = "tls"))]
let connector = service::connector(connector);

let connector = self.connector(connector);
Channel::new(connector, self.clone())
}

Expand Down
12 changes: 1 addition & 11 deletions tonic/src/transport/service/connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@ use std::task::{Context, Poll};
use tower::make::MakeConnection;
use tower_service::Service;

#[cfg(not(feature = "tls"))]
pub(crate) fn connector<C>(inner: C) -> Connector<C> {
Connector::new(inner)
}

#[cfg(feature = "tls")]
pub(crate) fn connector<C>(inner: C, tls: Option<TlsConnector>) -> Connector<C> {
Connector::new(inner, tls)
}

pub(crate) struct Connector<C> {
inner: C,
#[cfg(feature = "tls")]
Expand All @@ -34,7 +24,7 @@ impl<C> Connector<C> {
}

#[cfg(feature = "tls")]
fn new(inner: C, tls: Option<TlsConnector>) -> Self {
pub(crate) fn new(inner: C, tls: Option<TlsConnector>) -> Self {
Self { inner, tls }
}

Expand Down
7 changes: 1 addition & 6 deletions tonic/src/transport/service/discover.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::super::service;
use super::connection::Connection;
use crate::transport::Endpoint;

Expand Down Expand Up @@ -38,12 +37,8 @@ impl<K: Hash + Eq + Clone> Stream for DynamicServiceStream<K> {
http.set_keepalive(endpoint.tcp_keepalive);
http.set_connect_timeout(endpoint.connect_timeout);
http.enforce_http(false);
#[cfg(feature = "tls")]
let connector = service::connector(http, endpoint.tls.clone());

#[cfg(not(feature = "tls"))]
let connector = service::connector(http);
let connection = Connection::lazy(connector, endpoint);
let connection = Connection::lazy(endpoint.connector(http), endpoint);
let change = Ok(Change::Insert(k, connection));
Poll::Ready(Some(change))
}
Expand Down
2 changes: 1 addition & 1 deletion tonic/src/transport/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ mod user_agent;

pub(crate) use self::add_origin::AddOrigin;
pub(crate) use self::connection::Connection;
pub(crate) use self::connector::connector;
pub(crate) use self::connector::Connector;
pub(crate) use self::discover::DynamicServiceStream;
pub(crate) use self::executor::SharedExec;
pub(crate) use self::grpc_timeout::GrpcTimeout;
Expand Down

0 comments on commit 9123f68

Please sign in to comment.