diff --git a/rs/moq-relay/MIGRATE.md b/rs/moq-relay/MIGRATE.md index be2064a16ea..bbff7c893fc 100644 --- a/rs/moq-relay/MIGRATE.md +++ b/rs/moq-relay/MIGRATE.md @@ -19,7 +19,7 @@ Have the auth server mark cluster peers as `peer` (`moq auth serve --mtls-peer`) `stats.interval`, `--stats-interval`, and `MOQ_STATS_INTERVAL` take a duration such as `"1s"` or `"500ms"` instead of a bare number of seconds, and a zero interval refuses to start. Write `interval = 5` as `interval = "5s"`. -In Rust, `stats::Config::interval` is a `Duration`, and `stats::Config::build` returns a `Result`. +In Rust, `stats::Config::interval` is an `Option` (`None` publishes every second), and `stats::Config::build` returns a `Result`. ### `receive_window` has a floor (#5878) diff --git a/rs/moq-relay/src/config.rs b/rs/moq-relay/src/config.rs index 20c3d203142..76e92579f8c 100644 --- a/rs/moq-relay/src/config.rs +++ b/rs/moq-relay/src/config.rs @@ -933,6 +933,24 @@ root = ["ca.pem"] } } + /// Stats enabled from the CLI alone build at the producer's default interval: + /// the parser fills a field it skips with its type's zero, not `Default`. + #[tokio::test] + async fn cli_stats_build_without_an_interval() { + if !test_env::Env::without(&["MOQ_STATS_ENABLED", "MOQ_STATS_INTERVAL"]).isolate() { + return; + } + + let args = [ + std::ffi::OsString::from("moq-relay"), + std::ffi::OsString::from("--stats-enabled"), + ]; + let config = Config::parse_and_merge(args).expect("config load"); + assert!(config.stats.enabled); + let cluster = cluster::Cluster::new(cluster::Options::default()).expect("cluster"); + config.stats.build(cluster.origin.clone()).expect("stats build"); + } + /// Bare defaults loaded from TOML survive when the CLI does not mention them. #[test] fn cli_does_not_clobber_toml_stats_enabled() { @@ -960,7 +978,7 @@ linger = "2m" config.stats.enabled, "TOML's stats.enabled=true must not be clobbered by CLI defaults" ); - assert_eq!(config.stats.interval(), std::time::Duration::from_secs(5)); + assert_eq!(config.stats.interval(), Some(std::time::Duration::from_secs(5))); assert_eq!(config.stats.node.as_deref(), Some("localhost")); assert_eq!(config.stats.depth, 2); assert_eq!(config.stats.linger(), Some(std::time::Duration::from_secs(120))); @@ -973,7 +991,7 @@ linger = "2m" ]; let config = Config::parse_and_merge(args).expect("config load"); assert_eq!(config.stats.linger(), Some(std::time::Duration::from_secs(30))); - assert_eq!(config.stats.interval(), std::time::Duration::from_millis(250)); + assert_eq!(config.stats.interval(), Some(std::time::Duration::from_millis(250))); } /// Bare runtime defaults loaded from TOML survive when the CLI omits them. diff --git a/rs/moq-relay/src/stats.rs b/rs/moq-relay/src/stats.rs index e31011274d5..4e1b0e9c54e 100644 --- a/rs/moq-relay/src/stats.rs +++ b/rs/moq-relay/src/stats.rs @@ -49,7 +49,7 @@ pub struct Config { /// second. #[usage(skip)] #[serde(with = "humantime_serde")] - pub interval: Duration, + pub interval: Option, /// How often stats snapshots are published. Defaults to 1s. #[usage(long = "stats-interval", env = "MOQ_STATS_INTERVAL", setting = "stats.interval")] @@ -102,7 +102,7 @@ impl Default for Config { Self { enabled: false, prefix: ".stats".into(), - interval: Duration::from_secs(1), + interval: None, interval_arg: None, node: None, depth: 0, @@ -113,9 +113,9 @@ impl Default for Config { } impl Config { - /// The interval after command-line overrides. - pub(crate) fn interval(&self) -> Duration { - self.interval_arg.map(Into::into).unwrap_or(self.interval) + /// The interval after command-line overrides; `None` keeps the producer's default. + pub(crate) fn interval(&self) -> Option { + self.interval_arg.map(Into::into).or(self.interval) } /// The linger after command-line overrides; `None` keeps the producer's default. @@ -136,7 +136,7 @@ impl Config { } let prefix = self.prefix.clone(); let interval = self.interval(); - anyhow::ensure!(!interval.is_zero(), "stats.interval must be non-zero"); + anyhow::ensure!(interval != Some(Duration::ZERO), "stats.interval must be non-zero"); let node = self.node.clone().map(PathOwned::from); let depth = self.depth; let linger = self.linger(); @@ -144,9 +144,11 @@ impl Config { let mut config = moq_stats::produce::Config::new() .with_origin(origin) .with_prefix(prefix) - .with_interval(interval) .with_node(node) .with_depth(depth); + if let Some(interval) = interval { + config = config.with_interval(interval); + } if let Some(linger) = linger { config = config.with_linger(linger); } diff --git a/rs/moq-relay/tests/integration/wire_stats.rs b/rs/moq-relay/tests/integration/wire_stats.rs index 0c71c4ca0f5..e24b3149741 100644 --- a/rs/moq-relay/tests/integration/wire_stats.rs +++ b/rs/moq-relay/tests/integration/wire_stats.rs @@ -36,7 +36,7 @@ async fn wire_sources(io_uring: bool) { config.auth.public = vec![moq_auth::Pattern::all()]; config.stats.enabled = true; config.internal.listen = Some("127.0.0.1:0".parse().unwrap()); - config.stats.interval = Duration::from_secs(3600); + config.stats.interval = Some(Duration::from_secs(3600)); config.drain_timeout = Duration::ZERO; if io_uring { config.runtime.workers = Some(1);