Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated to notify 5.1 #152

Merged
merged 1 commit into from
Mar 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion limitador-server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ actix-web = "4.1"
actix-rt = "2"
paperclip = { version = "0.7", features = ["actix4"] }
serde = { version = "1", features = ["derive"] }
notify = "=5.0.0-pre.15"
notify = "5.1.0"
clap = "3.2"

[build-dependencies]
Expand Down
98 changes: 51 additions & 47 deletions limitador-server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,57 +302,61 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// structure needed to keep state of the last known canonical limits file path
let mut last_known_canonical_path = fs::canonicalize(&limit_file).unwrap();

let mut watcher = RecommendedWatcher::new(move |result: Result<Event, Error>| match result {
Ok(ref event) => {
match event.kind {
EventKind::Modify(ModifyKind::Data(_)) => {
// Content has been changed
// Usually happens in local or dockerized envs
let location = event.paths.first().unwrap().clone();

// Sometimes this event happens in k8s envs when
// content source is a configmap and it is replaced
// As the move event always occurrs,
// skip reloading limit file in this event.

// the parent dir is being watched
// only reload when the limits file content changed
if location == last_known_canonical_path {
let limiter = limiter.clone();
handle.spawn(async move {
match limiter.load_limits_from_file(&location).await {
Ok(_) => info!("data modified; reloaded limit file"),
Err(e) => error!("Failed reloading limit file: {}", e),
}
});
let mut watcher = RecommendedWatcher::new(
move |result: Result<Event, Error>| match result {
Ok(ref event) => {
match event.kind {
EventKind::Modify(ModifyKind::Data(_)) => {
// Content has been changed
// Usually happens in local or dockerized envs
let location = event.paths.first().unwrap().clone();

// Sometimes this event happens in k8s envs when
// content source is a configmap and it is replaced
// As the move event always occurrs,
// skip reloading limit file in this event.

// the parent dir is being watched
// only reload when the limits file content changed
if location == last_known_canonical_path {
let limiter = limiter.clone();
handle.spawn(async move {
match limiter.load_limits_from_file(&location).await {
Ok(_) => info!("data modified; reloaded limit file"),
Err(e) => error!("Failed reloading limit file: {}", e),
}
});
}
}
}
EventKind::Modify(ModifyKind::Name(RenameMode::Both)) => {
// Move operation occurred
// Usually happens in k8s envs when content source is a configmap

// symbolic links resolved.
let canonical_limit_file = fs::canonicalize(&limits_file_path_cloned).unwrap();
// check if the real path to the config file changed
// (eg: k8s ConfigMap replacement)
if canonical_limit_file != last_known_canonical_path {
last_known_canonical_path = canonical_limit_file.clone();
let limiter = limiter.clone();
handle.spawn(async move {
match limiter.load_limits_from_file(&canonical_limit_file).await {
Ok(_) => info!("file moved; reloaded limit file"),
Err(e) => error!("Failed reloading limit file: {}", e),
}
});
EventKind::Modify(ModifyKind::Name(RenameMode::Both)) => {
// Move operation occurred
// Usually happens in k8s envs when content source is a configmap

// symbolic links resolved.
let canonical_limit_file =
fs::canonicalize(&limits_file_path_cloned).unwrap();
// check if the real path to the config file changed
// (eg: k8s ConfigMap replacement)
if canonical_limit_file != last_known_canonical_path {
last_known_canonical_path = canonical_limit_file.clone();
let limiter = limiter.clone();
handle.spawn(async move {
match limiter.load_limits_from_file(&canonical_limit_file).await {
Ok(_) => info!("file moved; reloaded limit file"),
Err(e) => error!("Failed reloading limit file: {}", e),
}
});
}
}
_ => (), // /dev/null
}
_ => (), // /dev/null
}
}
Err(ref e) => {
warn!("Something went wrong while watching limit file: {}", e);
}
})?;
Err(ref e) => {
warn!("Something went wrong while watching limit file: {}", e);
}
},
notify::Config::default(),
)?;
watcher.watch(limits_file_dir, RecursiveMode::Recursive)?;

info!("Envoy RLS server starting on {}", envoy_rls_address);
Expand Down