roowhod: move config normalization to config.rs
Build and test / build (push) Successful in 1m45s
Build and test / test (push) Successful in 2m19s
Build and test / check (push) Successful in 2m30s
Build and test / docs (push) Successful in 4m8s

This commit is contained in:
2026-07-20 23:46:37 +09:00
parent 4373eec6b5
commit 026c19acc7
2 changed files with 47 additions and 34 deletions
+6 -33
View File
@@ -3,6 +3,7 @@ use std::{
os::fd::{AsRawFd, FromRawFd, OwnedFd},
path::PathBuf,
sync::Arc,
time::Duration,
};
use anyhow::Context;
@@ -21,9 +22,6 @@ use roowho2_lib::server::{
varlink_api::varlink_client_server_task,
};
/// Default maximum number of distinct hosts to keep rwhod status records for at the same time.
const DEFAULT_MAX_STATUS_ENTRIES: usize = 4096;
#[derive(Parser)]
#[command(
author = "Programvareverkstedet <projects@pvv.ntnu.no>",
@@ -80,19 +78,9 @@ async fn main() -> anyhow::Result<()> {
let mut join_set = tokio::task::JoinSet::new();
let max_status_entries = match config.rwhod.max_status_entries {
Some(0) => {
tracing::warn!(
"rwhod.max_status_entries is set to 0, remapping to default value of {}",
DEFAULT_MAX_STATUS_ENTRIES
);
DEFAULT_MAX_STATUS_ENTRIES
}
Some(max_status_entries) => max_status_entries,
None => DEFAULT_MAX_STATUS_ENTRIES,
};
let whod_status_store: RwhodStatusStore =
Arc::new(RwLock::new(RwhodStatusRegistry::new(max_status_entries)));
let whod_status_store: RwhodStatusStore = Arc::new(RwLock::new(RwhodStatusRegistry::new(
config.rwhod.max_status_entries(),
)));
let client_server_token = CancellationToken::new();
let client_server_token_ = client_server_token.clone();
@@ -122,7 +110,7 @@ async fn main() -> anyhow::Result<()> {
whod_status_store.clone(),
rwhod_ignore_list.clone(),
config.rwhod.interfaces.clone(),
config.rwhod.send_interval_seconds,
config.rwhod.send_interval(),
));
} else {
tracing::debug!("RWHOD server is disabled in configuration");
@@ -154,32 +142,17 @@ async fn ctrl_c_handler() -> anyhow::Result<()> {
.map_err(|e| anyhow::anyhow!("Failed to listen for Ctrl-C: {}", e))
}
/// Default interval, in seconds, between rwhod status packet broadcasts.
const DEFAULT_SEND_INTERVAL_SECONDS: u64 = 60;
async fn rwhod_server(
socket: UdpSocket,
whod_status_store: RwhodStatusStore,
ignore_list: Option<IgnoreList>,
allowed_interfaces: Option<HashSet<String>>,
send_interval_seconds: Option<u64>,
send_interval: Duration,
) -> anyhow::Result<()> {
let socket = Arc::new(socket);
let interfaces =
roowho2_lib::server::rwhod::determine_relevant_interfaces(allowed_interfaces.as_ref())?;
let send_interval_seconds = match send_interval_seconds {
Some(0) => {
tracing::warn!(
"rwhod.send_interval_seconds is set to 0, remapping to default value of {} seconds",
DEFAULT_SEND_INTERVAL_SECONDS
);
DEFAULT_SEND_INTERVAL_SECONDS
}
Some(seconds) => seconds,
None => DEFAULT_SEND_INTERVAL_SECONDS,
};
let send_interval = std::time::Duration::from_secs(send_interval_seconds);
let sender_task =
rwhod_packet_sender_task(socket.clone(), interfaces, ignore_list, send_interval);
let receiver_task = rwhod_packet_receiver_task(socket.clone(), whod_status_store);
+41 -1
View File
@@ -1,4 +1,4 @@
use std::{collections::HashSet, path::PathBuf};
use std::{collections::HashSet, path::PathBuf, time::Duration};
use serde::{Deserialize, Serialize};
@@ -6,6 +6,12 @@ pub const DEFAULT_CONFIG_PATH: &str = "/etc/roowho2/config.toml";
pub const DEFAULT_CLIENT_SOCKET_PATH: &str = "/run/roowho2/server_client.sock";
/// Default interval between rwhod status packet broadcasts.
const DEFAULT_SEND_INTERVAL_SECONDS: u64 = 60;
/// Default maximum number of distinct hosts to keep rwhod status records for at the same time.
const DEFAULT_MAX_STATUS_ENTRIES: usize = 4096;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Config {
/// Logging level for the daemon.
@@ -57,6 +63,40 @@ pub struct RwhodConfig {
pub max_status_entries: Option<usize>,
}
impl RwhodConfig {
/// Resolves [`Self::send_interval_seconds`] into a concrete interval.
pub fn send_interval(&self) -> Duration {
let seconds = match self.send_interval_seconds {
Some(0) => {
tracing::warn!(
"rwhod.send_interval_seconds is set to 0, remapping to default value of {} seconds",
DEFAULT_SEND_INTERVAL_SECONDS
);
DEFAULT_SEND_INTERVAL_SECONDS
}
Some(seconds) => seconds,
None => DEFAULT_SEND_INTERVAL_SECONDS,
};
Duration::from_secs(seconds)
}
/// Resolves [`Self::max_status_entries`] into a concrete limit.
pub fn max_status_entries(&self) -> usize {
match self.max_status_entries {
Some(0) => {
tracing::warn!(
"rwhod.max_status_entries is set to 0, remapping to default value of {}",
DEFAULT_MAX_STATUS_ENTRIES
);
DEFAULT_MAX_STATUS_ENTRIES
}
Some(max_status_entries) => max_status_entries,
None => DEFAULT_MAX_STATUS_ENTRIES,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FingerdConfig {
/// Enable or disable the fingerd server functionality.