use debouncer

Signed-off-by: tison <wander4096@gmail.com>
This commit is contained in:
tison
2024-03-22 19:39:45 +08:00
parent 1e83ab8e2a
commit 4f71cd1598
4 changed files with 45 additions and 10 deletions

24
Cargo.lock generated
View File

@@ -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"

View File

@@ -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 = [

View File

@@ -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

View File

@@ -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::<notify::Result<notify::Event>>();
let mut watcher = notify::recommended_watcher(tx).context(FileWatchSnafu)?;
watcher
let (tx, rx) = channel::<DebounceEventResult>();
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;
}