From dac1cdb91687171001df9c5bba3cb7f4231a08d4 Mon Sep 17 00:00:00 2001 From: Michiel Date: Fri, 10 Jan 2025 11:09:35 +0100 Subject: [PATCH] Change measurement noise to f64 instead of NtpDuration Prevents rounding errors when using from_seconds, especially for small values (<1e-9) --- ntpd/src/daemon/config/ntp_source.rs | 5 ++--- ntpd/src/daemon/spawn/pps.rs | 5 ++--- ntpd/src/daemon/spawn/sock.rs | 5 ++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/ntpd/src/daemon/config/ntp_source.rs b/ntpd/src/daemon/config/ntp_source.rs index 17c28f7b6..99487d4eb 100644 --- a/ntpd/src/daemon/config/ntp_source.rs +++ b/ntpd/src/daemon/config/ntp_source.rs @@ -7,7 +7,6 @@ use std::{ }; use ntp_proto::tls_utils::Certificate; -use ntp_proto::NtpDuration; #[cfg(feature = "unstable_ntpv5")] use ntp_proto::NtpVersion; use serde::{de, Deserialize, Deserializer}; @@ -114,14 +113,14 @@ pub struct NtsPoolSourceConfig { #[serde(deny_unknown_fields)] pub struct SockSourceConfig { pub path: PathBuf, - pub measurement_noise_estimate: NtpDuration, + pub measurement_noise_estimate: f64, } #[derive(Deserialize, Debug, PartialEq, Clone)] #[serde(deny_unknown_fields)] pub struct PpsSourceConfig { pub path: PathBuf, - pub measurement_noise_estimate: NtpDuration, + pub measurement_noise_estimate: f64, #[serde(default = "default_period")] pub period: f64, } diff --git a/ntpd/src/daemon/spawn/pps.rs b/ntpd/src/daemon/spawn/pps.rs index 9d51f2244..02ca82f4a 100644 --- a/ntpd/src/daemon/spawn/pps.rs +++ b/ntpd/src/daemon/spawn/pps.rs @@ -37,7 +37,7 @@ impl Spawner for PpsSpawner { SpawnAction::Create(SourceCreateParameters::Pps(PpsSourceCreateParameters { id: SourceId::new(), path: self.config.path.clone(), - noise_estimate: self.config.measurement_noise_estimate.to_seconds(), + noise_estimate: self.config.measurement_noise_estimate, period: self.config.period, })), )) @@ -75,7 +75,6 @@ impl Spawner for PpsSpawner { #[cfg(test)] mod tests { - use ntp_proto::NtpDuration; use tokio::sync::mpsc; use crate::{ @@ -93,7 +92,7 @@ mod tests { let noise_estimate = 1e-6; let mut spawner = PpsSpawner::new(PpsSourceConfig { path: socket_path.clone(), - measurement_noise_estimate: NtpDuration::from_seconds(noise_estimate), + measurement_noise_estimate: noise_estimate, period: 1., }); let spawner_id = spawner.get_id(); diff --git a/ntpd/src/daemon/spawn/sock.rs b/ntpd/src/daemon/spawn/sock.rs index fa68a2691..4530c3e96 100644 --- a/ntpd/src/daemon/spawn/sock.rs +++ b/ntpd/src/daemon/spawn/sock.rs @@ -37,7 +37,7 @@ impl Spawner for SockSpawner { SpawnAction::Create(SourceCreateParameters::Sock(SockSourceCreateParameters { id: SourceId::new(), path: self.config.path.clone(), - noise_estimate: self.config.measurement_noise_estimate.to_seconds(), + noise_estimate: self.config.measurement_noise_estimate, })), )) .await?; @@ -74,7 +74,6 @@ impl Spawner for SockSpawner { #[cfg(test)] mod tests { - use ntp_proto::NtpDuration; use tokio::sync::mpsc; use crate::{ @@ -92,7 +91,7 @@ mod tests { let noise_estimate = 1e-6; let mut spawner = SockSpawner::new(SockSourceConfig { path: socket_path.clone(), - measurement_noise_estimate: NtpDuration::from_seconds(noise_estimate), + measurement_noise_estimate: noise_estimate, }); let spawner_id = spawner.get_id(); let (action_tx, mut action_rx) = mpsc::channel(MESSAGE_BUFFER_SIZE);