@@ -9,7 +9,7 @@ use bytes::Bytes;
99use http:: { uri:: Uri , HeaderValue } ;
1010use hyper:: rt;
1111use 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 } ;
1313use 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