use std::sync::Arc; use tokio::sync::Mutex; use crate::{Error, Result}; pub struct NetworkLeash { lock: Arc> } impl NetworkLeash { pub fn new() -> Self { tracing::trace!("Creating new NetworkLeash"); Self { lock: Arc::new(Mutex::new(())), } } pub async fn with_interface(&self, name: &str, operation: F) -> Result where F: FnOnce(&mut ifconfig::Iface) -> Result + Send + 'static, R: Send + 'static, { let _guard = self.lock.lock().await; let name = name.to_string(); tokio::task::spawn_blocking(move || { let mut iface = ifconfig::Iface::new(&name)?; operation(&mut iface) }).await? } pub async fn gated(&self, operation: F) -> Result where F: FnOnce() -> Result + Send + 'static, R: Send + 'static, { let _guard = self.lock.lock().await; tokio::task::spawn_blocking(move || { operation() }).await? } }