From efcdf4c45614aa1f14ed4f10ccee1a9b0fbb87e0 Mon Sep 17 00:00:00 2001 From: Luna Yao <40349250+ZnqbuZ@users.noreply.github.com> Date: Sat, 21 Feb 2026 23:24:17 +0100 Subject: [PATCH] utils: add DirtyState --- easytier/src/dns/client.rs | 2 +- easytier/src/dns/client_mgr.rs | 16 +++++----------- easytier/src/dns/peer_mgr.rs | 16 ++++++---------- easytier/src/dns/server.rs | 11 ++++------- easytier/src/dns/utils.rs | 18 ++++++++++++++++++ 5 files changed, 34 insertions(+), 29 deletions(-) diff --git a/easytier/src/dns/client.rs b/easytier/src/dns/client.rs index 7dc44d97..ece4bed3 100644 --- a/easytier/src/dns/client.rs +++ b/easytier/src/dns/client.rs @@ -48,7 +48,7 @@ impl DnsClient { ..Default::default() }; loop { - self.mgr.dirty.notified().await; + self.mgr.dirty.notify.notified().await; if let Err(e) = self.heartbeat(&mut rpc, &mut heartbeat).await { tracing::error!("DnsClient heartbeat failed: {:?}", e); } diff --git a/easytier/src/dns/client_mgr.rs b/easytier/src/dns/client_mgr.rs index f9a813e0..4a99d55c 100644 --- a/easytier/src/dns/client_mgr.rs +++ b/easytier/src/dns/client_mgr.rs @@ -1,4 +1,4 @@ -use crate::dns::utils::{DirtyFlag, NameServerAddr}; +use crate::dns::utils::{DirtyFlag, DirtyState, NameServerAddr}; use crate::dns::zone::{Zone, ZoneGroup}; use crate::proto::dns::DnsClientMgrRpc; use crate::proto::dns::{DnsSnapshot, HeartbeatRequest, HeartbeatResponse}; @@ -6,13 +6,11 @@ use crate::proto::rpc_types; use crate::proto::rpc_types::controller::BaseController; use crate::utils::{DeterministicDigest, MapTryInto}; use anyhow::Error; -use derive_more::{Deref, DerefMut}; use hickory_server::authority::Catalog; use itertools::Itertools; use moka::future::Cache; use std::collections::HashSet; use std::time::Duration; -use tokio::sync::Notify; use uuid::Uuid; #[derive(Debug, Clone, Default)] @@ -38,21 +36,17 @@ impl TryFrom<&DnsSnapshot> for DnsClientInfo { const DNS_CLIENT_TTL: Duration = Duration::from_secs(5); -// TODO: same as DnsPeerMgrDirtyState -#[derive(Debug, Default, Deref, DerefMut)] -pub struct DnsClientMgrDirtyState { +#[derive(Debug, Default)] +pub struct DnsClientMgrDirtyFlags { pub(super) catalog: DirtyFlag, pub(super) addresses: DirtyFlag, pub(super) listeners: DirtyFlag, - #[deref] - #[deref_mut] - notify: Notify, } #[derive(Debug)] pub struct DnsClientMgr { clients: Cache, - pub(super) dirty: DnsClientMgrDirtyState, + pub(super) dirty: DirtyState, } impl DnsClientMgr { @@ -150,7 +144,7 @@ impl DnsClientMgrRpc for DnsClientMgr { } self.clients.insert(id, new).await; - self.dirty.notify_one(); + self.dirty.notify.notify_one(); } false } else { diff --git a/easytier/src/dns/peer_mgr.rs b/easytier/src/dns/peer_mgr.rs index 0422abc1..3af82c52 100644 --- a/easytier/src/dns/peer_mgr.rs +++ b/easytier/src/dns/peer_mgr.rs @@ -1,7 +1,7 @@ use crate::common::config::ConfigLoader; use crate::common::PeerId; use crate::dns::config::{DnsExportConfig, DnsGlobalCtxExt}; -use crate::dns::utils::DirtyFlag; +use crate::dns::utils::{DirtyFlag, DirtyState}; use crate::dns::zone::ZoneGroup; use crate::peer_center::instance::PeerCenterPeerManagerTrait; use crate::peers::peer_manager::PeerManager; @@ -13,12 +13,11 @@ use crate::proto::rpc_types; use crate::proto::rpc_types::controller::BaseController; use crate::utils::DeterministicDigest; use anyhow::Context; -use derive_more::{Deref, DerefMut}; +use derive_more::Deref; use itertools::Itertools; use moka::future::Cache; use std::sync::Arc; use std::time::Duration; -use tokio::sync::Notify; #[derive(Debug, Clone)] pub struct DnsPeerInfo { @@ -40,18 +39,15 @@ impl TryFrom for DnsPeerInfo { const DNS_PEER_TTL: Duration = Duration::from_secs(3); -#[derive(Debug, Default, Deref, DerefMut)] -pub struct DnsPeerMgrDirtyState { +#[derive(Debug, Default)] +pub struct DnsPeerMgrDirtyFlags { pub(crate) peers: DirtyFlag, - #[deref] - #[deref_mut] - notify: Notify, } #[derive(Debug, Deref)] pub struct DnsPeerMgr { peers: Cache, - pub(super) dirty: DnsPeerMgrDirtyState, + pub(super) dirty: DirtyState, #[deref] mgr: Arc, @@ -112,7 +108,7 @@ impl DnsPeerMgr { } } - self.dirty.notify_one(); + self.dirty.notify.notify_one(); } async fn fetch(&self, peer_id: PeerId) -> anyhow::Result { diff --git a/easytier/src/dns/server.rs b/easytier/src/dns/server.rs index 23df18cb..4f2005b1 100644 --- a/easytier/src/dns/server.rs +++ b/easytier/src/dns/server.rs @@ -1,7 +1,5 @@ -use super::{utils::NameServerAddr, zone::Zone}; -use crate::common::PeerId; +use super::utils::NameServerAddr; use crate::dns::client_mgr::DnsClientMgr; -use cidr::Ipv4Inet; use derivative::Derivative; use derive_more::{Deref, DerefMut, From, Into}; use hickory_proto::rr::Record; @@ -20,9 +18,8 @@ use std::{sync::Arc, time::Duration}; use tokio::net::{TcpListener, UdpSocket}; use tokio::{sync::RwLock, task::JoinHandle}; use tokio_util::sync::CancellationToken; -use crate::dns::peer_mgr::DnsPeerMgr; use crate::peers::peer_manager::PeerManager; -use crate::proto::dns::{DnsClientMgrRpcServer, DnsPeerMgrRpcServer}; +use crate::proto::dns::DnsClientMgrRpcServer; #[derive(Clone)] pub struct DynamicCatalog { @@ -214,7 +211,7 @@ impl DnsServer { let dirty = &self.mgr.dirty; let mut runtime = None; loop { - dirty.notified().await; + dirty.notify.notified().await; if dirty.catalog.reset() { self.catalog.replace(self.mgr.catalog()).await; @@ -231,7 +228,7 @@ impl DnsServer { { tracing::error!("failed to reload listeners: {:?}", e); dirty.listeners.mark(); - dirty.notify_one(); + dirty.notify.notify_one(); } } diff --git a/easytier/src/dns/utils.rs b/easytier/src/dns/utils.rs index 728b385a..2eca4187 100644 --- a/easytier/src/dns/utils.rs +++ b/easytier/src/dns/utils.rs @@ -18,6 +18,7 @@ use std::fmt::{Display, Formatter}; use std::net::{IpAddr, SocketAddr}; use std::str::FromStr; use std::sync::atomic::{AtomicBool, Ordering}; +use tokio::sync::Notify; use url::Url; pub fn sanitize(name: &str) -> String { @@ -267,6 +268,23 @@ where } } +#[derive(Debug, Deref, DerefMut)] +pub(super) struct DirtyState { + #[deref] + #[deref_mut] + flags: T, + pub notify: Notify, +} + +impl Default for DirtyState { + fn default() -> Self { + Self { + flags: T::default(), + notify: Notify::new(), + } + } +} + #[derive(Debug)] pub(super) struct DirtyFlag(AtomicBool);