utils: add DirtyState

This commit is contained in:
Luna Yao
2026-02-21 23:24:17 +01:00
parent e9a0e4f042
commit efcdf4c456
5 changed files with 34 additions and 29 deletions
+1 -1
View File
@@ -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);
}
+5 -11
View File
@@ -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<Uuid, DnsClientInfo>,
pub(super) dirty: DnsClientMgrDirtyState,
pub(super) dirty: DirtyState<DnsClientMgrDirtyFlags>,
}
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 {
+6 -10
View File
@@ -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<DnsExportConfig> 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<PeerId, DnsPeerInfo>,
pub(super) dirty: DnsPeerMgrDirtyState,
pub(super) dirty: DirtyState<DnsPeerMgrDirtyFlags>,
#[deref]
mgr: Arc<PeerManager>,
@@ -112,7 +108,7 @@ impl DnsPeerMgr {
}
}
self.dirty.notify_one();
self.dirty.notify.notify_one();
}
async fn fetch(&self, peer_id: PeerId) -> anyhow::Result<DnsPeerInfo> {
+4 -7
View File
@@ -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();
}
}
+18
View File
@@ -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<T> {
#[deref]
#[deref_mut]
flags: T,
pub notify: Notify,
}
impl<T: Default> Default for DirtyState<T> {
fn default() -> Self {
Self {
flags: T::default(),
notify: Notify::new(),
}
}
}
#[derive(Debug)]
pub(super) struct DirtyFlag(AtomicBool);