refactor: restructure PTP client to use thread-safe runtime and RNG

Co-authored-by: aider (openrouter/anthropic/claude-sonnet-4) <aider@aider.chat>
This commit is contained in:
Chaos Rogers 2025-07-10 18:54:20 +01:00
parent d6ca055170
commit 7b4deb1cf8
2 changed files with 10 additions and 7 deletions

View file

@ -67,13 +67,15 @@ async fn main() {
}); });
} }
// 5⃣ Spawn PTP client task // 5⃣ Spawn PTP client thread (blocking, not async)
{ {
let ptp_state = ltc_state.clone(); let ptp_state = ltc_state.clone();
let config_clone = config_arc.clone(); let config_clone = config_arc.clone();
tokio::spawn(async move { thread::spawn(move || {
println!("🚀 PTP task launched"); println!("🚀 PTP thread launched");
start_ptp_client(ptp_state, config_clone).await; // Create a new tokio runtime for this thread
let rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(start_ptp_client(ptp_state, config_clone));
}); });
} }

View file

@ -1,6 +1,6 @@
use crate::config::Config; use crate::config::Config;
use crate::sync_logic::LtcState; use crate::sync_logic::LtcState;
use rand::thread_rng; use rand::{rngs::StdRng, SeedableRng};
use statime::{ use statime::{
config::{ config::{
AcceptAnyMaster, ClockIdentity, DelayMechanism, InstanceConfig, PortConfig, AcceptAnyMaster, ClockIdentity, DelayMechanism, InstanceConfig, PortConfig,
@ -117,8 +117,9 @@ async fn run_ptp_session(
let event_socket = create_socket(&interface, 319)?; let event_socket = create_socket(&interface, 319)?;
let general_socket = create_socket(&interface, 320)?; let general_socket = create_socket(&interface, 320)?;
// 6. Add port and run BMCA // 6. Add port and run BMCA (use StdRng which is Send)
let mut port = ptp_instance.add_port(port_config, filter_config, clock, thread_rng()); let rng = StdRng::from_entropy();
let mut port = ptp_instance.add_port(port_config, filter_config, clock, rng);
ptp_instance.bmca(&mut [&mut port]); ptp_instance.bmca(&mut [&mut port]);
let (mut running_port, initial_actions) = port.end_bmca(); let (mut running_port, initial_actions) = port.end_bmca();