Skip to content

Commit 80193c7

Browse files
just-an-engineersylvestre
authored andcommitted
Add ability to specify server timeout amount in ms from config file
1 parent 330d160 commit 80193c7

File tree

5 files changed

+61
-8
lines changed

5 files changed

+61
-8
lines changed

src/commands.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ pub fn run_command(cmd: Command) -> Result<i32> {
609609
// Config isn't required for all commands, but if it's broken then we should flag
610610
// it early and loudly.
611611
let config = &Config::load()?;
612-
let startup_timeout = config.server_startup_timeout;
612+
let startup_timeout = config.server_timing.startup_timeout;
613613

614614
match cmd {
615615
Command::ShowStats(fmt, advanced) => {

src/config.rs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,9 +555,18 @@ impl Default for DistConfig {
555555
pub struct FileConfig {
556556
pub cache: CacheConfigs,
557557
pub dist: DistConfig,
558+
// pub timing: ServerTimingConfig,
558559
pub server_startup_timeout_ms: Option<u64>,
560+
pub server_shutdown_timeout_ms: Option<u64>,
561+
pub port: Option<u16>,
559562
}
560563

564+
// #[derive(Debug, Default, Serialize, Deserialize, Eq, PartialEq)]
565+
// pub struct ServerTimingConfig {
566+
// pub server_startup_timeout_ms: Option<u64>,
567+
// pub server_shutdown_timeout_ms: Option<u64>,
568+
// }
569+
561570
// If the file doesn't exist or we can't read it, log the issue and proceed. If the
562571
// config exists but doesn't parse then something is wrong - return an error.
563572
pub fn try_read_config_file<T: DeserializeOwned>(path: &Path) -> Result<Option<T>> {
@@ -945,7 +954,14 @@ pub struct Config {
945954
pub cache: Option<CacheType>,
946955
pub fallback_cache: DiskCacheConfig,
947956
pub dist: DistConfig,
948-
pub server_startup_timeout: Option<std::time::Duration>,
957+
pub server_timing: ServerTiming,
958+
pub port: Option<u16>,
959+
}
960+
961+
#[derive(Debug, Default, PartialEq, Eq)]
962+
pub struct ServerTiming {
963+
pub startup_timeout: Option<std::time::Duration>,
964+
pub shutdown_timeout: Option<std::time::Duration>,
949965
}
950966

951967
impl Config {
@@ -967,11 +983,20 @@ impl Config {
967983
cache,
968984
dist,
969985
server_startup_timeout_ms,
986+
server_shutdown_timeout_ms,
987+
port,
970988
} = file_conf;
971989
conf_caches.merge(cache);
972990

973991
let server_startup_timeout =
974992
server_startup_timeout_ms.map(std::time::Duration::from_millis);
993+
let server_shutdown_timeout =
994+
server_shutdown_timeout_ms.map(std::time::Duration::from_millis);
995+
let server_timing = ServerTiming {
996+
startup_timeout: server_startup_timeout,
997+
shutdown_timeout: server_shutdown_timeout,
998+
};
999+
9751000

9761001
let EnvConfig { cache } = env_conf;
9771002
conf_caches.merge(cache);
@@ -981,7 +1006,8 @@ impl Config {
9811006
cache: caches,
9821007
fallback_cache,
9831008
dist,
984-
server_startup_timeout,
1009+
server_timing,
1010+
port,
9851011
}
9861012
}
9871013
}
@@ -1281,6 +1307,8 @@ fn config_overrides() {
12811307
},
12821308
dist: Default::default(),
12831309
server_startup_timeout_ms: None,
1310+
server_shutdown_timeout_ms: None,
1311+
port: None,
12841312
};
12851313

12861314
assert_eq!(
@@ -1302,7 +1330,8 @@ fn config_overrides() {
13021330
rw_mode: CacheModeConfig::ReadWrite,
13031331
},
13041332
dist: Default::default(),
1305-
server_startup_timeout: None,
1333+
server_timing: Default::default(),
1334+
port: None,
13061335
}
13071336
);
13081337
}
@@ -1577,7 +1606,26 @@ no_credentials = true
15771606
toolchain_cache_size: 5368709120,
15781607
rewrite_includes_only: false,
15791608
},
1580-
server_startup_timeout_ms: Some(10000),
1609+
server_startup_timeout_ms: Some(10_000),
1610+
server_shutdown_timeout_ms: None,
1611+
port: None,
1612+
}
1613+
)
1614+
}
1615+
1616+
#[test]
1617+
fn test_port_config() {
1618+
// just set up a config file with just port, then have it read it in, and ensure port is defined in the struct
1619+
const CONFIG_STR: &str = "port = 8080";
1620+
let file_config: FileConfig = toml::from_str(CONFIG_STR).expect("Is valid toml.");
1621+
assert_eq!(
1622+
file_config,
1623+
FileConfig {
1624+
cache: Default::default(),
1625+
dist: Default::default(),
1626+
server_startup_timeout_ms: None,
1627+
server_shutdown_timeout_ms: None,
1628+
port: Some(8080),
15811629
}
15821630
)
15831631
}

src/server.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -658,11 +658,12 @@ impl<C: CommandCreatorSync> SccacheServer<C> {
658658
}
659659
})?;
660660

661-
const SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(10);
661+
let config = Config::load().unwrap_or_default();
662+
let shutdown_timeout: Duration = config.server_timing.shutdown_timeout.unwrap_or(Duration::from_secs(10));
662663
info!(
663664
"moving into the shutdown phase now, waiting at most {} seconds \
664665
for all client requests to complete",
665-
SHUTDOWN_TIMEOUT.as_secs()
666+
shutdown_timeout.as_secs()
666667
);
667668

668669
// Once our server has shut down either due to inactivity or a manual
@@ -672,7 +673,7 @@ impl<C: CommandCreatorSync> SccacheServer<C> {
672673
//
673674
// Note that we cap the amount of time this can take, however, as we
674675
// don't want to wait *too* long.
675-
runtime.block_on(async { time::timeout(SHUTDOWN_TIMEOUT, wait).await })?;
676+
runtime.block_on(async { time::timeout(shutdown_timeout, wait).await })?;
676677

677678
info!("ok, fully shutting down now");
678679

tests/harness/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ pub fn sccache_client_cfg(
165165
rewrite_includes_only: false, // TODO
166166
},
167167
server_startup_timeout_ms: None,
168+
server_shutdown_timeout_ms: None,
169+
port: None,
168170
}
169171
}
170172

tests/oauth.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ fn config_with_dist_auth(
6060
rewrite_includes_only: true,
6161
},
6262
server_startup_timeout_ms: None,
63+
server_shutdown_timeout_ms: None,
64+
port: None,
6365
}
6466
}
6567

0 commit comments

Comments
 (0)