feat(util): Introduce Mutex/RwLock error handling traits

This commit is contained in:
Nils Andreas Svee committed 2026-02-22 16:57:02 +07:00
1 parent 8589a1c7dc
commit d4ad24ce4a
6 files changed
+33 -1

No files matched your search

+1
View File
@@ -1,4 +1,5 @@
use crate::pinger::PingReply;
use crate::util::MutexExt;
use crate::Config;
use log::info;
use std::collections::HashMap;
+1
View File
@@ -10,6 +10,7 @@ mod pinger_icmp_ts;
mod ratecontroller;
mod reflector_selector;
mod time;
mod util;
use crate::baseliner::{Baseliner, ReflectorStats};
use ::log::{debug, info};
+2 -1
View File
@@ -1,7 +1,8 @@
use crate::MeasurementType;
use icmp_socket::socket::IcmpSocket;
use icmp_socket::{IcmpSocket4, Icmpv4Packet};
use log::{debug};
use crate::util::RwLockExt;
use log::{debug, warn};
use std::net::{IpAddr, Ipv4Addr};
use std::sync::mpsc::Sender;
use std::sync::{Arc, RwLock};
+1
View File
@@ -1,5 +1,6 @@
use crate::netlink::{Netlink, NetlinkError, Qdisc};
use crate::time::Time;
use crate::util::{MutexExt, RwLockExt};
use crate::{Config, ReflectorStats};
use log::{debug, info, warn};
use rustix::thread::ClockId;
+1
View File
@@ -1,3 +1,4 @@
use crate::util::{MutexExt, RwLockExt};
use crate::{Config, ReflectorStats};
use log::{debug, info};
use std::collections::HashMap;
+27
View File
@@ -0,0 +1,27 @@
use anyhow::anyhow;
use std::sync::{Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockWriteGuard};
pub trait MutexExt<T> {
fn lock_anyhow(&self) -> anyhow::Result<MutexGuard<'_, T>>;
}
impl<T> MutexExt<T> for Mutex<T> {
fn lock_anyhow(&self) -> anyhow::Result<MutexGuard<'_, T>> {
self.lock().map_err(|e| anyhow!("mutex poisoned: {e}"))
}
}
pub trait RwLockExt<T> {
fn read_anyhow(&self) -> anyhow::Result<RwLockReadGuard<'_, T>>;
fn write_anyhow(&self) -> anyhow::Result<RwLockWriteGuard<'_, T>>;
}
impl<T> RwLockExt<T> for RwLock<T> {
fn read_anyhow(&self) -> anyhow::Result<RwLockReadGuard<'_, T>> {
self.read().map_err(|e| anyhow!("rwlock poisoned: {e}"))
}
fn write_anyhow(&self) -> anyhow::Result<RwLockWriteGuard<'_, T>> {
self.write().map_err(|e| anyhow!("rwlock poisoned: {e}"))
}
}