mirror of
https://github.com/EasyTier/EasyTier.git
synced 2026-09-02 09:09:17 +00:00
utils(dirty): replace Notify with watch
This commit is contained in:
@@ -166,7 +166,7 @@ impl DnsNode {
|
|||||||
last_heartbeat = Instant::now();
|
last_heartbeat = Instant::now();
|
||||||
}
|
}
|
||||||
|
|
||||||
_ = self.mgr.dirty.notified() => {}
|
_ = self.mgr.dirty.wait() => {}
|
||||||
|
|
||||||
event = subscriber.recv() => {
|
event = subscriber.recv() => {
|
||||||
match event {
|
match event {
|
||||||
|
|||||||
@@ -75,7 +75,6 @@ impl DnsPeerMgrInner {
|
|||||||
pub async fn refresh(&self, peer_id: PeerId) {
|
pub async fn refresh(&self, peer_id: PeerId) {
|
||||||
if peer_id == self.peer_mgr.my_peer_id() {
|
if peer_id == self.peer_mgr.my_peer_id() {
|
||||||
self.dirty.mark();
|
self.dirty.mark();
|
||||||
self.dirty.notify_one();
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,25 +90,23 @@ impl DnsPeerMgrInner {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.dirty.mark();
|
let mut invalidate = route.dns.is_empty();
|
||||||
|
|
||||||
let invalidate = route.dns.is_empty()
|
if !invalidate {
|
||||||
|| match self.fetch(peer_id).await {
|
match self.fetch(peer_id).await {
|
||||||
Ok(info) => {
|
Ok(info) => self.peers.insert(peer_id, info).await,
|
||||||
self.peers.insert(peer_id, info).await;
|
|
||||||
false
|
|
||||||
}
|
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
tracing::warn!(%peer_id, ?error, "failed to fetch dns export config from peer");
|
tracing::warn!(%peer_id, ?error, "failed to fetch dns export config from peer");
|
||||||
true
|
invalidate = true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
if invalidate {
|
if invalidate {
|
||||||
self.peers.invalidate(&peer_id).await;
|
self.peers.invalidate(&peer_id).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.dirty.notify_one();
|
self.dirty.mark();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(skip(self), level = "trace", ret)]
|
#[instrument(skip(self), level = "trace", ret)]
|
||||||
|
|||||||
@@ -225,7 +225,7 @@ impl DnsServer {
|
|||||||
|
|
||||||
let reload_catalog = async {
|
let reload_catalog = async {
|
||||||
loop {
|
loop {
|
||||||
dirty.catalog.notified().await;
|
dirty.catalog.wait().await;
|
||||||
if dirty.catalog.reset() {
|
if dirty.catalog.reset() {
|
||||||
self.catalog.replace(self.mgr.catalog()).await;
|
self.catalog.replace(self.mgr.catalog()).await;
|
||||||
}
|
}
|
||||||
@@ -235,7 +235,7 @@ impl DnsServer {
|
|||||||
|
|
||||||
let reload_addresses = async {
|
let reload_addresses = async {
|
||||||
loop {
|
loop {
|
||||||
dirty.addresses.notified().await;
|
dirty.addresses.wait().await;
|
||||||
if dirty.addresses.reset() {
|
if dirty.addresses.reset() {
|
||||||
if let Err(e) = self.reload_addresses(self.mgr.iter_addresses()).await {
|
if let Err(e) = self.reload_addresses(self.mgr.iter_addresses()).await {
|
||||||
tracing::error!("failed to reload addresses: {:?}", e);
|
tracing::error!("failed to reload addresses: {:?}", e);
|
||||||
@@ -248,7 +248,7 @@ impl DnsServer {
|
|||||||
|
|
||||||
let reload_listeners = async {
|
let reload_listeners = async {
|
||||||
loop {
|
loop {
|
||||||
dirty.listeners.notified().await;
|
dirty.listeners.wait().await;
|
||||||
if dirty.listeners.reset() {
|
if dirty.listeners.reset() {
|
||||||
if let Err(e) = self
|
if let Err(e) = self
|
||||||
.reload_listeners(self.mgr.iter_listeners(), &mut runtime)
|
.reload_listeners(self.mgr.iter_listeners(), &mut runtime)
|
||||||
|
|||||||
@@ -1,39 +1,32 @@
|
|||||||
use derive_more::Deref;
|
use tokio::sync::watch;
|
||||||
use std::sync::atomic::{AtomicBool, Ordering};
|
|
||||||
use tokio::sync::Notify;
|
|
||||||
|
|
||||||
#[derive(Debug, Deref)]
|
#[derive(Debug)]
|
||||||
pub struct DirtyFlag {
|
pub struct DirtyFlag {
|
||||||
dirty: AtomicBool,
|
tx: watch::Sender<bool>,
|
||||||
#[deref]
|
rx: watch::Receiver<bool>,
|
||||||
notify: Notify,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DirtyFlag {
|
impl DirtyFlag {
|
||||||
pub fn new(value: bool) -> Self {
|
pub fn new(value: bool) -> Self {
|
||||||
let notify = Notify::new();
|
let (tx, rx) = watch::channel(value);
|
||||||
|
Self { tx, rx }
|
||||||
if value {
|
|
||||||
notify.notify_one();
|
|
||||||
}
|
|
||||||
|
|
||||||
Self {
|
|
||||||
dirty: AtomicBool::new(value),
|
|
||||||
notify,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn mark(&self) {
|
pub fn mark(&self) {
|
||||||
self.dirty.store(true, Ordering::Release);
|
self.tx.send(true).ok();
|
||||||
self.notify.notify_one();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn peek(&self) -> bool {
|
pub fn peek(&self) -> bool {
|
||||||
self.dirty.load(Ordering::Acquire)
|
*self.tx.borrow()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn reset(&self) -> bool {
|
pub fn reset(&self) -> bool {
|
||||||
self.dirty.swap(false, Ordering::Acquire)
|
self.tx.send_replace(false)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn wait(&self) {
|
||||||
|
let mut rx = self.rx.clone();
|
||||||
|
let _ = rx.wait_for(|v| *v).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user