-
Notifications
You must be signed in to change notification settings - Fork 623
Add timeout to tcp connections #2231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,18 @@ use byteorder::{BigEndian, ByteOrder}; | |
| use retry::{delay::Fixed, retry}; | ||
| use std::io::{self, BufReader, BufWriter, Read}; | ||
| use std::net::TcpStream; | ||
| use std::time::Duration; | ||
| use std::{env, net::ToSocketAddrs}; | ||
|
|
||
| const DEFAULT_CONNECTION_TIMEOUT: u64 = 20; | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be much lower, but leaving it this high aligns it more closely with the current behavior. I'm running it with
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like 5, 8, or 10 seems appropriate IMO. No connection between any two computers takes that long unless there is very heavy bufferbloat, packet loss, or an interplanetary connection. |
||
|
|
||
| /// Get the time clients should try to connect to the server before continuing, in seconds. | ||
| pub(crate) fn get_connection_timeout() -> u64 { | ||
| env::var("SCCACHE_CONNECTION_TIMEOUT") | ||
| .ok() | ||
| .and_then(|s| s.parse().ok()) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think warning on an invalid value as early as possible would be good. |
||
| .unwrap_or(DEFAULT_CONNECTION_TIMEOUT) | ||
| } | ||
|
|
||
| /// A connection to an sccache server. | ||
| pub struct ServerConnection { | ||
|
|
@@ -65,7 +77,10 @@ impl ServerConnection { | |
| /// Establish a TCP connection to an sccache server listening on `port`. | ||
| pub fn connect_to_server(port: u16) -> io::Result<ServerConnection> { | ||
| trace!("connect_to_server({})", port); | ||
| let stream = TcpStream::connect(("127.0.0.1", port))?; | ||
| let stream = TcpStream::connect_timeout( | ||
| &("127.0.0.1", port).to_socket_addrs()?.next().unwrap(), | ||
| Duration::from_secs(get_connection_timeout()), | ||
| )?; | ||
| ServerConnection::new(stream) | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, this idea looks good to me. However, I think it should be a config value in the
[dist]section instead of an environment variable. Most distribution configurations aren't exposed to the environment for now.The current name is a bit confusing from my point of view. I would expect something like
tcp_connect_timeout.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should
connect_to_serverinsrc/client.rsand the functions that use it (connect_or_start_serverinsrc/command.rs) take the config struct as an argument?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It depends, I'd say just the timeout is enough. If we start to see a need for a lot more config values, sub-structs might do as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think SCCACHE_CONNECT_TIMEOUT is better. The interpretations that first come to mind for me are: