use crate::proxy::{read_ndjson, Proxy};
use crate::server::Pool;
use std::collections::BTreeSet;
use std::io::BufReader;
use std::net::IpAddr;
use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio_util::sync::CancellationToken;
const DEBOUNCE: Duration = Duration::from_millis(250);
const POLL: Duration = Duration::from_millis(50);
pub fn reconcile(pool: &Pool, desired: Vec<Proxy>) {
let current = pool.addrs();
let want: BTreeSet<(IpAddr, u16)> = desired.iter().map(|p| (p.host, p.port)).collect();
for (host, port) in ¤t {
if !want.contains(&(*host, *port)) {
pool.remove_addr(*host, *port);
}
}
for proxy in desired {
if !current.contains(&(proxy.host, proxy.port)) {
pool.add(proxy);
}
}
}
pub struct WatchHandle {
_watcher: notify::RecommendedWatcher,
cancel: CancellationToken,
}
impl WatchHandle {
pub fn shutdown(&self) {
self.cancel.cancel();
}
}
impl Drop for WatchHandle {
fn drop(&mut self) {
self.cancel.cancel();
}
}
pub fn spawn_watch(pool: Arc<Pool>, path: PathBuf) -> std::io::Result<WatchHandle> {
use notify::{RecursiveMode, Watcher};
let cancel = CancellationToken::new();
let (tx, rx) = std::sync::mpsc::channel::<notify::Result<notify::Event>>();
let mut watcher = notify::recommended_watcher(move |res| {
let _ = tx.send(res);
})
.map_err(to_io)?;
let dir = path.parent().filter(|p| !p.as_os_str().is_empty());
let dir = dir.unwrap_or_else(|| Path::new("."));
watcher
.watch(dir, RecursiveMode::NonRecursive)
.map_err(to_io)?;
let target = path.file_name().map(|n| n.to_os_string());
let task_cancel = cancel.clone();
tokio::spawn(async move {
let mut pending: Option<Instant> = None;
loop {
tokio::select! {
_ = task_cancel.cancelled() => break,
_ = tokio::time::sleep(POLL) => {}
}
let mut touched = false;
while let Ok(res) = rx.try_recv() {
if let Ok(event) = res {
if event
.paths
.iter()
.any(|p| p.file_name().map(|n| n.to_os_string()) == target)
{
touched = true;
}
}
}
if touched {
pending = Some(Instant::now()); }
if let Some(since) = pending {
if since.elapsed() >= DEBOUNCE {
pending = None;
match reload(&path) {
Ok(desired) => reconcile(&pool, desired),
Err(e) => tracing::warn!(
path = %path.display(),
error = %e,
"watch: re-parse failed, keeping current pool"
),
}
}
}
}
});
Ok(WatchHandle {
_watcher: watcher,
cancel,
})
}
fn reload(path: &Path) -> std::io::Result<Vec<Proxy>> {
read_ndjson(BufReader::new(std::fs::File::open(path)?))
}
fn to_io(e: notify::Error) -> std::io::Error {
std::io::Error::other(e)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::server::PoolConfig;
use crate::types::Proto;
fn proxy(ip: &str, port: u16) -> Proxy {
Proxy::new(ip.parse().unwrap(), port, BTreeSet::from([Proto::Http]))
}
#[test]
fn reconcile_removes_then_adds_disjoint_sets() {
let pool = Pool::from_proxies(vec![proxy("1.1.1.1", 1)], PoolConfig::default());
reconcile(&pool, vec![proxy("2.2.2.2", 2)]);
assert_eq!(
pool.addrs(),
BTreeSet::from([("2.2.2.2".parse().unwrap(), 2)])
);
}
}