Skip to content

Commit

Permalink
feat(channel): Add local_address as an option Endpoint builder (#1567)
Browse files Browse the repository at this point in the history
* Adds local_address as an option Endpoint builder.

* Apply suggestions from code review

Co-authored-by: tottoto <[email protected]>

---------

Co-authored-by: tottoto <[email protected]>
  • Loading branch information
jparris and tottoto authored Jan 16, 2025
1 parent 5ad89bf commit 6839a39
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion tonic/src/transport/channel/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bytes::Bytes;
use http::{uri::Uri, HeaderValue};
use hyper::rt;
use hyper_util::client::legacy::connect::HttpConnector;
use std::{fmt, future::Future, pin::Pin, str::FromStr, time::Duration};
use std::{fmt, future::Future, net::IpAddr, pin::Pin, str::FromStr, time::Duration};
use tower_service::Service;

/// Channel builder.
Expand All @@ -36,6 +36,7 @@ pub struct Endpoint {
pub(crate) http2_max_header_list_size: Option<u32>,
pub(crate) connect_timeout: Option<Duration>,
pub(crate) http2_adaptive_window: Option<bool>,
pub(crate) local_address: Option<IpAddr>,
pub(crate) executor: SharedExec,
}

Expand Down Expand Up @@ -325,12 +326,23 @@ impl Endpoint {
)
}

/// Set the local address.
///
/// This sets the IP address the client will use. By default we let hyper select the IP address.
pub fn local_address(self, addr: Option<IpAddr>) -> Self {
Endpoint {
local_address: addr,
..self
}
}

pub(crate) fn http_connector(&self) -> service::Connector<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.set_local_address(self.local_address);
self.connector(http)
}

Expand Down Expand Up @@ -452,6 +464,7 @@ impl From<Uri> for Endpoint {
connect_timeout: None,
http2_adaptive_window: None,
executor: SharedExec::tokio(),
local_address: None,
}
}
}
Expand Down

0 comments on commit 6839a39

Please sign in to comment.