Skip to content

Commit 6839a39

Browse files
jparristottoto
andauthored
feat(channel): Add local_address as an option Endpoint builder (#1567)
* Adds local_address as an option Endpoint builder. * Apply suggestions from code review Co-authored-by: tottoto <tottotodev@gmail.com> --------- Co-authored-by: tottoto <tottotodev@gmail.com>
1 parent 5ad89bf commit 6839a39

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

tonic/src/transport/channel/endpoint.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use bytes::Bytes;
99
use http::{uri::Uri, HeaderValue};
1010
use hyper::rt;
1111
use hyper_util::client::legacy::connect::HttpConnector;
12-
use std::{fmt, future::Future, pin::Pin, str::FromStr, time::Duration};
12+
use std::{fmt, future::Future, net::IpAddr, pin::Pin, str::FromStr, time::Duration};
1313
use tower_service::Service;
1414

1515
/// Channel builder.
@@ -36,6 +36,7 @@ pub struct Endpoint {
3636
pub(crate) http2_max_header_list_size: Option<u32>,
3737
pub(crate) connect_timeout: Option<Duration>,
3838
pub(crate) http2_adaptive_window: Option<bool>,
39+
pub(crate) local_address: Option<IpAddr>,
3940
pub(crate) executor: SharedExec,
4041
}
4142

@@ -325,12 +326,23 @@ impl Endpoint {
325326
)
326327
}
327328

329+
/// Set the local address.
330+
///
331+
/// This sets the IP address the client will use. By default we let hyper select the IP address.
332+
pub fn local_address(self, addr: Option<IpAddr>) -> Self {
333+
Endpoint {
334+
local_address: addr,
335+
..self
336+
}
337+
}
338+
328339
pub(crate) fn http_connector(&self) -> service::Connector<HttpConnector> {
329340
let mut http = HttpConnector::new();
330341
http.enforce_http(false);
331342
http.set_nodelay(self.tcp_nodelay);
332343
http.set_keepalive(self.tcp_keepalive);
333344
http.set_connect_timeout(self.connect_timeout);
345+
http.set_local_address(self.local_address);
334346
self.connector(http)
335347
}
336348

@@ -452,6 +464,7 @@ impl From<Uri> for Endpoint {
452464
connect_timeout: None,
453465
http2_adaptive_window: None,
454466
executor: SharedExec::tokio(),
467+
local_address: None,
455468
}
456469
}
457470
}

0 commit comments

Comments
 (0)