@@ -39,23 +39,13 @@ pub enum TargetAddr {
3939/// with an IPv6 CIDR and a fallback IP address.
4040#[ derive( Clone ) ]
4141pub struct Connector {
42- /// Optional IPv6 CIDR (Classless Inter-Domain Routing), used to optionally
43- /// configure an IPv6 address.
4442 cidr : Option < IpCidr > ,
45-
46- /// Optional CIDR range for IP addresses.
4743 cidr_range : Option < u8 > ,
48-
49- /// Optional IP address as a fallback option in case of connection failure.
5044 fallback : Option < Fallback > ,
51-
52- /// Connect timeout in milliseconds.
5345 connect_timeout : Duration ,
54-
55- /// Enable SO_REUSEADDR for outbond connection socket
46+ # [ cfg ( any ( target_os = "android" , target_os = "fuchsia" , target_os = "linux" ) ) ]
47+ tcp_user_timeout : Option < Duration > ,
5648 reuseaddr : Option < bool > ,
57-
58- /// Default http connector
5949 http : connect:: HttpConnector ,
6050}
6151
@@ -134,6 +124,8 @@ impl Connector {
134124 cidr_range : Option < u8 > ,
135125 fallback : Option < Fallback > ,
136126 connect_timeout : u64 ,
127+ #[ cfg( any( target_os = "android" , target_os = "fuchsia" , target_os = "linux" ) ) ]
128+ tcp_user_timeout : Option < u64 > ,
137129 reuseaddr : Option < bool > ,
138130 ) -> Self {
139131 let connect_timeout = Duration :: from_secs ( connect_timeout) ;
@@ -147,8 +139,10 @@ impl Connector {
147139 cidr_range,
148140 fallback,
149141 connect_timeout,
150- http : http_connector,
142+ #[ cfg( any( target_os = "android" , target_os = "fuchsia" , target_os = "linux" ) ) ]
143+ tcp_user_timeout : tcp_user_timeout. map ( Duration :: from_secs) ,
151144 reuseaddr,
145+ http : http_connector,
152146 }
153147 }
154148
@@ -204,20 +198,28 @@ impl TcpConnector<'_> {
204198
205199 /// Creates a [`TcpSocket`] and binds it to an IP address within the provided CIDR range.
206200 async fn create_socket_with_cidr ( & self , cidr : IpCidr ) -> std:: io:: Result < TcpSocket > {
207- match cidr {
201+ let socket = match cidr {
208202 IpCidr :: V4 ( cidr) => {
209203 let socket = TcpSocket :: new_v4 ( ) ?;
210204 let addr = assign_ipv4_from_extension ( cidr, self . inner . cidr_range , self . extension ) ;
211205 socket. bind ( SocketAddr :: new ( IpAddr :: V4 ( addr) , 0 ) ) ?;
212- Ok ( socket)
206+ socket
213207 }
214208 IpCidr :: V6 ( cidr) => {
215209 let socket = TcpSocket :: new_v6 ( ) ?;
216210 let addr = assign_ipv6_from_extension ( cidr, self . inner . cidr_range , self . extension ) ;
217211 socket. bind ( SocketAddr :: new ( IpAddr :: V6 ( addr) , 0 ) ) ?;
218- Ok ( socket)
212+ socket
219213 }
214+ } ;
215+
216+ #[ cfg( any( target_os = "android" , target_os = "fuchsia" , target_os = "linux" ) ) ]
217+ if let Some ( tcp_user_timeout) = self . inner . tcp_user_timeout {
218+ let socket_ref = socket2:: SockRef :: from ( & socket) ;
219+ socket_ref. set_tcp_user_timeout ( Some ( tcp_user_timeout) ) ?;
220220 }
221+
222+ Ok ( socket)
221223 }
222224
223225 /// Creates a [`TcpSocket`] and binds it to the fallback address.
@@ -289,10 +291,17 @@ impl TcpConnector<'_> {
289291 } ;
290292
291293 socket. set_nodelay ( true ) ?;
294+
292295 if let Some ( reuseaddr) = self . inner . reuseaddr {
293296 socket. set_reuseaddr ( reuseaddr) ?;
294297 }
295298
299+ #[ cfg( any( target_os = "android" , target_os = "fuchsia" , target_os = "linux" ) ) ]
300+ if let Some ( tcp_user_timeout) = self . inner . tcp_user_timeout {
301+ let socket_ref = socket2:: SockRef :: from ( & socket) ;
302+ socket_ref. set_tcp_user_timeout ( Some ( tcp_user_timeout) ) ?;
303+ }
304+
296305 Ok ( socket)
297306 }
298307
@@ -672,10 +681,16 @@ impl HttpConnector<'_> {
672681 }
673682
674683 connector. set_nodelay ( true ) ;
684+
675685 if let Some ( reuseaddr) = self . inner . reuseaddr {
676686 connector. set_reuse_address ( reuseaddr) ;
677687 }
678688
689+ #[ cfg( any( target_os = "android" , target_os = "fuchsia" , target_os = "linux" ) ) ]
690+ if let Some ( tcp_user_timeout) = self . inner . tcp_user_timeout {
691+ connector. set_tcp_user_timeout ( Some ( tcp_user_timeout) ) ;
692+ }
693+
679694 Client :: builder ( TokioExecutor :: new ( ) )
680695 . timer ( TokioTimer :: new ( ) )
681696 . http1_title_case_headers ( true )
0 commit comments