From cccd68dff5fb328097417a78018160427de276ba Mon Sep 17 00:00:00 2001 From: tottoto Date: Sun, 5 Jan 2025 12:10:16 +0900 Subject: [PATCH] chore(channel): Add helper to create http connector (#2121) --- tonic/src/transport/channel/endpoint.rs | 15 +++++++-------- tonic/src/transport/channel/service/discover.rs | 8 +------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/tonic/src/transport/channel/endpoint.rs b/tonic/src/transport/channel/endpoint.rs index 5b97ebeda..9de93f7cd 100644 --- a/tonic/src/transport/channel/endpoint.rs +++ b/tonic/src/transport/channel/endpoint.rs @@ -325,14 +325,18 @@ impl Endpoint { ) } - /// Create a channel from this config. - pub async fn connect(&self) -> Result { + pub(crate) fn http_connector(&self) -> HttpConnector { let mut http = HttpConnector::new(); http.enforce_http(false); http.set_nodelay(self.tcp_nodelay); http.set_keepalive(self.tcp_keepalive); http.set_connect_timeout(self.connect_timeout); + http + } + /// Create a channel from this config. + pub async fn connect(&self) -> Result { + let http = self.http_connector(); let connector = self.connector(http); Channel::connect(connector, self.clone()).await @@ -343,12 +347,7 @@ impl Endpoint { /// The channel returned by this method does not attempt to connect to the endpoint until first /// use. pub fn connect_lazy(&self) -> Channel { - let mut http = HttpConnector::new(); - http.enforce_http(false); - http.set_nodelay(self.tcp_nodelay); - http.set_keepalive(self.tcp_keepalive); - http.set_connect_timeout(self.connect_timeout); - + let http = self.http_connector(); let connector = self.connector(http); Channel::new(connector, self.clone()) diff --git a/tonic/src/transport/channel/service/discover.rs b/tonic/src/transport/channel/service/discover.rs index 2c03f8084..43f007158 100644 --- a/tonic/src/transport/channel/service/discover.rs +++ b/tonic/src/transport/channel/service/discover.rs @@ -1,6 +1,5 @@ use super::super::{Connection, Endpoint}; -use hyper_util::client::legacy::connect::HttpConnector; use std::{ hash::Hash, pin::Pin, @@ -38,12 +37,7 @@ impl Stream for DynamicServiceStream { Poll::Pending | Poll::Ready(None) => Poll::Pending, Poll::Ready(Some(change)) => match change { Change::Insert(k, endpoint) => { - let mut http = HttpConnector::new(); - http.set_nodelay(endpoint.tcp_nodelay); - http.set_keepalive(endpoint.tcp_keepalive); - http.set_connect_timeout(endpoint.connect_timeout); - http.enforce_http(false); - + let http = endpoint.http_connector(); let connection = Connection::lazy(endpoint.connector(http), endpoint); let change = Ok(TowerChange::Insert(k, connection)); Poll::Ready(Some(change))