@@ -19,6 +19,18 @@ use byteorder::{BigEndian, ByteOrder};
19
19
use retry:: { delay:: Fixed , retry} ;
20
20
use std:: io:: { self , BufReader , BufWriter , Read } ;
21
21
use std:: net:: TcpStream ;
22
+ use std:: time:: Duration ;
23
+ use std:: { env, net:: ToSocketAddrs } ;
24
+
25
+ const DEFAULT_CONNECTION_TIMEOUT : u64 = 20 ;
26
+
27
+ /// Get the time clients should try to connect to the server before continuing, in seconds.
28
+ pub ( crate ) fn get_connection_timeout ( ) -> u64 {
29
+ env:: var ( "SCCACHE_CONNECTION_TIMEOUT" )
30
+ . ok ( )
31
+ . and_then ( |s| s. parse ( ) . ok ( ) )
32
+ . unwrap_or ( DEFAULT_CONNECTION_TIMEOUT )
33
+ }
22
34
23
35
/// A connection to an sccache server.
24
36
pub struct ServerConnection {
@@ -65,7 +77,10 @@ impl ServerConnection {
65
77
/// Establish a TCP connection to an sccache server listening on `port`.
66
78
pub fn connect_to_server ( port : u16 ) -> io:: Result < ServerConnection > {
67
79
trace ! ( "connect_to_server({})" , port) ;
68
- let stream = TcpStream :: connect ( ( "127.0.0.1" , port) ) ?;
80
+ let stream = TcpStream :: connect_timeout (
81
+ & ( "127.0.0.1" , port) . to_socket_addrs ( ) ?. next ( ) . unwrap ( ) ,
82
+ Duration :: from_secs ( get_connection_timeout ( ) ) ,
83
+ ) ?;
69
84
ServerConnection :: new ( stream)
70
85
}
71
86
0 commit comments