Skip to content

Commit

Permalink
chore(channel): Add helper to create http connector (#2121)
Browse files Browse the repository at this point in the history
  • Loading branch information
tottoto authored Jan 5, 2025
1 parent 84cb7b7 commit cccd68d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 15 deletions.
15 changes: 7 additions & 8 deletions tonic/src/transport/channel/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,14 +325,18 @@ impl Endpoint {
)
}

/// Create a channel from this config.
pub async fn connect(&self) -> Result<Channel, Error> {
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<Channel, Error> {
let http = self.http_connector();
let connector = self.connector(http);

Channel::connect(connector, self.clone()).await
Expand All @@ -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())
Expand Down
8 changes: 1 addition & 7 deletions tonic/src/transport/channel/service/discover.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::super::{Connection, Endpoint};

use hyper_util::client::legacy::connect::HttpConnector;
use std::{
hash::Hash,
pin::Pin,
Expand Down Expand Up @@ -38,12 +37,7 @@ impl<K: Hash + Eq + Clone> Stream for DynamicServiceStream<K> {
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))
Expand Down

0 comments on commit cccd68d

Please sign in to comment.