Page MenuHomePhabricator

leash.rs
No OneTemporary

leash.rs

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

File Metadata

Mime Type
text/plain
Expires
Sun, Jun 8, 8:26 AM (18 h, 56 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
47582
Default Alt Text
leash.rs (1 KB)

Event Timeline