Skip to content
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

Add timeout to tcp connections #2231

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ configuration variables
* `SCCACHE_CONF` configuration file path
* `SCCACHE_CACHED_CONF`
* `SCCACHE_IDLE_TIMEOUT` how long the local daemon process waits for more client requests before exiting, in seconds. Set to `0` to run sccache permanently
* `SCCACHE_CONNECTION_TIMEOUT` how long clients should try to connect to the server before continuing, in seconds.
Copy link
Collaborator

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should connect_to_server in src/client.rs and the functions that use it (connect_or_start_server in src/command.rs) take the config struct as an argument?

* `SCCACHE_STARTUP_NOTIFY` specify a path to a socket which will be used for server completion notification
* `SCCACHE_MAX_FRAME_LENGTH` how much data can be transferred between client and server
* `SCCACHE_NO_DAEMON` set to `1` to disable putting the server to the background
Expand Down
17 changes: 16 additions & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 2 locally.


/// 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())
.unwrap_or(DEFAULT_CONNECTION_TIMEOUT)
}

/// A connection to an sccache server.
pub struct ServerConnection {
Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions src/dist/client_auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use tokio::runtime::Runtime;
use url::Url;
use uuid::Uuid;

use crate::errors::*;
use crate::{client::get_connection_timeout, errors::*};

// These (arbitrary) ports need to be registered as valid redirect urls in the oauth provider you're using
pub const VALID_PORTS: &[u16] = &[12731, 32492, 56909];
Expand Down Expand Up @@ -503,7 +503,7 @@ async fn try_bind() -> Result<HyperBuilderWrap> {
.expect("Expected at least one address in parsed socket address");

// Hyper binds with reuseaddr and reuseport so binding won't fail as you'd expect on Linux
match TcpStream::connect(addr) {
match TcpStream::connect_timeout(&addr, Duration::from_secs(get_connection_timeout())) {
// Already open
Ok(_) => continue,
// Doesn't seem to be open
Expand Down