cancellation

This commit is contained in:
2026-02-04 03:13:39 -08:00
parent 156df9ae86
commit 08871a5782
10 changed files with 268 additions and 141 deletions

View File

@@ -4,7 +4,9 @@ use std::path::PathBuf;
use async_trait::async_trait;
use libc::{c_double, c_int, timeval};
use tracing::debug;
use tokio::select;
use tokio_util::sync::CancellationToken;
use tracing::{debug, warn};
use crate::{ChimemonMessage, ChimemonTarget, ChimemonTargetChannel, ChronySockConfig};
@@ -35,41 +37,46 @@ impl ChronySockServer {
#[async_trait]
impl ChimemonTarget for ChronySockServer {
async fn run(mut self, mut chan: ChimemonTargetChannel) {
async fn run(mut self, mut chan: ChimemonTargetChannel, cancel: CancellationToken) {
loop {
let msg = chan.recv().await.unwrap();
match msg {
ChimemonMessage::TimeReport(tr) => {
if tr.valid {
{
let frame = ChronyTimeReport {
tv: timeval {
tv_sec: TryInto::<libc::time_t>::try_into(
tr.system_time.timestamp(),
)
.unwrap(),
tv_usec: tr.system_time.timestamp_subsec_micros()
as libc::suseconds_t,
},
offset: tr.offset.num_nanoseconds().unwrap() as f64 / 1e9,
leap: if tr.leap_flag { 1 } else { 0 },
pulse: 0,
_pad: 0,
magic: CHRONY_MAGIC,
};
let bs = unsafe {
std::slice::from_raw_parts(
(&frame as *const ChronyTimeReport) as *const u8,
mem::size_of::<ChronyTimeReport>(),
)
};
debug!("Sending to chrony sock {:#?}", frame);
let sock = UnixDatagram::unbound().unwrap();
sock.send_to(bs, &self.sock_path).unwrap();
}
select! {
_ = cancel.cancelled() => { return }
msg = chan.recv() => {
match msg {
Ok(ChimemonMessage::TimeReport(tr)) => {
if tr.valid {
{
let frame = ChronyTimeReport {
tv: timeval {
tv_sec: TryInto::<libc::time_t>::try_into(
tr.system_time.timestamp(),
)
.unwrap(),
tv_usec: tr.system_time.timestamp_subsec_micros()
as libc::suseconds_t,
},
offset: tr.offset.num_nanoseconds().unwrap() as f64 / 1e9,
leap: if tr.leap_flag { 1 } else { 0 },
pulse: 0,
_pad: 0,
magic: CHRONY_MAGIC,
};
let bs = unsafe {
std::slice::from_raw_parts(
(&frame as *const ChronyTimeReport) as *const u8,
mem::size_of::<ChronyTimeReport>(),
)
};
debug!("Sending to chrony sock {:#?}", frame);
let sock = UnixDatagram::unbound().unwrap();
sock.send_to(bs, &self.sock_path).unwrap();
}
}
},
Err(e) => warn!("Error receiving from channel: {}", e.to_string()),
_ => continue,
}
}
_ => continue,
}
}
}