From 4f71cd15988c9a3ee705aaf29692af9bf8c9bfe8 Mon Sep 17 00:00:00 2001 From: tison Date: Fri, 22 Mar 2024 19:39:45 +0800 Subject: [PATCH] use debouncer Signed-off-by: tison --- Cargo.lock | 24 +++++++++++++++ Cargo.toml | 1 + src/auth/Cargo.toml | 1 + .../user_provider/watch_file_user_provider.rs | 29 ++++++++++++------- 4 files changed, 45 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 35bcabe50c..f182d3411b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -706,6 +706,7 @@ dependencies = [ "digest", "hex", "notify", + "notify-debouncer-full", "secrecy", "sha1", "snafu", @@ -3396,6 +3397,15 @@ dependencies = [ "tokio", ] +[[package]] +name = "file-id" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6584280525fb2059cba3db2c04abf947a1a29a45ddae89f3870f8281704fafc9" +dependencies = [ + "windows-sys 0.48.0", +] + [[package]] name = "filetime" version = "0.2.23" @@ -5825,6 +5835,20 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "notify-debouncer-full" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f5dab59c348b9b50cf7f261960a20e389feb2713636399cd9082cd4b536154" +dependencies = [ + "crossbeam-channel", + "file-id", + "log", + "notify", + "parking_lot 0.12.1", + "walkdir", +] + [[package]] name = "ntapi" version = "0.4.1" diff --git a/Cargo.toml b/Cargo.toml index f3752ebcc9..bcf65f8209 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -111,6 +111,7 @@ meter-core = { git = "https://github.com/GreptimeTeam/greptime-meter.git", rev = mockall = "0.11.4" moka = "0.12" notify = "6.1" +notify-debouncer-full = "0.3" num_cpus = "1.16" once_cell = "1.18" opentelemetry-proto = { git = "https://github.com/waynexia/opentelemetry-rust.git", rev = "33841b38dda79b15f2024952be5f32533325ca02", features = [ diff --git a/src/auth/Cargo.toml b/src/auth/Cargo.toml index db8c200545..d98aee2145 100644 --- a/src/auth/Cargo.toml +++ b/src/auth/Cargo.toml @@ -20,6 +20,7 @@ common-telemetry.workspace = true digest = "0.10" hex = { version = "0.4" } notify.workspace = true +notify-debouncer-full.workspace = true secrecy = { version = "0.8", features = ["serde", "alloc"] } sha1 = "0.10" snafu.workspace = true diff --git a/src/auth/src/user_provider/watch_file_user_provider.rs b/src/auth/src/user_provider/watch_file_user_provider.rs index 196d9ad2f6..1d6ab477bf 100644 --- a/src/auth/src/user_provider/watch_file_user_provider.rs +++ b/src/auth/src/user_provider/watch_file_user_provider.rs @@ -1,16 +1,19 @@ use std::collections::HashMap; use std::path::Path; -use std::sync::mpsc::channel; use std::sync::{Arc, Mutex}; +use std::sync::mpsc::channel; +use std::time::Duration; use async_trait::async_trait; -use common_telemetry::info; use notify::{EventKind, RecursiveMode, Watcher}; +use notify_debouncer_full::{DebounceEventResult, new_debouncer}; use snafu::ResultExt; +use common_telemetry::info; + +use crate::{Identity, Password, UserInfoRef, UserProvider}; use crate::error::{FileWatchSnafu, Result}; use crate::user_provider::{authenticate_with_credential, load_credential_from_file}; -use crate::{Identity, Password, UserInfoRef, UserProvider}; pub(crate) const WATCH_FILE_USER_PROVIDER: &str = "watch_file_user_provider"; @@ -26,18 +29,24 @@ impl WatchFileUserProvider { users: users.clone(), }; - let (tx, rx) = channel::>(); - let mut watcher = notify::recommended_watcher(tx).context(FileWatchSnafu)?; - watcher + let (tx, rx) = channel::(); + + let mut debouncer = + new_debouncer(Duration::from_secs(1), None, tx).context(FileWatchSnafu)?; + debouncer + .watcher() .watch(Path::new(filepath), RecursiveMode::NonRecursive) .context(FileWatchSnafu)?; let filepath = filepath.to_string(); std::thread::spawn(move || { - let _watcher = watcher; + let _hold = debouncer; while let Ok(res) = rx.recv() { - if let Ok(event) = res { - if matches!(event.kind, EventKind::Modify(_) | EventKind::Create(_)) { - info!("Detected user provider file change: {:?}", event); + if let Ok(events) = res { + if events + .iter() + .any(|e| matches!(e.kind, EventKind::Modify(_) | EventKind::Create(_))) + { + info!("User provider file {} changed", &filepath); if let Ok(credential) = load_credential_from_file(&filepath) { *users.lock().expect("users credential must be valid") = credential; }