Skip to content

Commit

Permalink
Configuration loading overhaul
Browse files Browse the repository at this point in the history
- Simple merging logic for `Config`
- Load config from system-wide locations (fixes #30)
- Dynamically pick up changes from newly created config files (fixes #29)
- Use `directories-rs` to find user level config directory path

A new `ConfigWatcher` is introduced, which opportunistically watches the
closest existing parent of target paths and rewatches dynamically accordingly.
This way we can get notifications for new file creations in addition to
modifications.

`ConfigWatcher` also does debounce (100ms by default) to only trigger
one config reload for multiple events in a short time period.

The core idea of the watcher is as follow:

Given a watching target path `/base/sub/config.toml`,
a) readd watches if any segment in the path is created/removed;
b) reload config only if the event is about the full target path.

The actual implementation extends this idea to handle multiple targets.
Please refer to `config_watcher.rs` for details and extensive test cases.

A few design points:

- Notify event handling is done in a separate thread, rather than in
  notify's internal event handler thread, because the code is more
  readable and calling unwatch/watch in that thread may lead to deadlock
  in some cases. (notify-rs/notify#410, notify-rs/notify#463)
- For simplicity, `ConfigWatcher` doesn't  provide infomation about
  which exact config file was changed, assuming a reload would need to
  read all config files anyway.
  • Loading branch information
Aetf committed Jun 19, 2024
1 parent cc410ae commit 1563d9c
Show file tree
Hide file tree
Showing 6 changed files with 974 additions and 68 deletions.
87 changes: 87 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions libshpool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ tempfile = "3" # RAII tmp files
strip-ansi-escapes = "0.2.0" # cleaning up strings for pager display
notify = "6" # watch config file for updates
libproc = "0.14.8" # sniffing shells by examining the subprocess
directories = "5.0.1" # get paths for configurations

# rusty wrapper for unix apis
[dependencies.nix]
Expand All @@ -55,3 +56,4 @@ features = ["std", "fmt", "tracing-log", "smallvec"]

[dev-dependencies]
ntest = "0.9" # test timeouts
assert_matches = "1.5" # assert_matches macro
Loading

0 comments on commit 1563d9c

Please sign in to comment.