Skip to main content

dynamic_config/watch/
handle.rs

1//! Starting a watcher, and the handle that stops it.
2//!
3//! The registry (one watcher per [`WatchKey`] — a type, or one `Dynamic`
4//! instance), the spawn that registers *before* returning so no edit slips
5//! through the gap, the rollback that frees the key when a spawn fails
6//! partway, and the directory-level watches — directories, not files,
7//! because editors and atomic saves replace the inode.
8
9use std::any::TypeId;
10use std::collections::BTreeMap;
11use std::path::{Path, PathBuf};
12use std::sync::{mpsc, Mutex};
13use std::thread;
14use std::time::Duration;
15
16use notify::{Event, RecursiveMode, Watcher};
17
18use crate::error::Error;
19use crate::log::warning;
20
21use super::debounce::run;
22use super::{WatchMode, Watched};
23
24/// What a watcher is watched *as*: one per type, or one per instance.
25///
26/// A type's identity is its [`TypeId`] — the one identity that survives
27/// generics; the display name is kept only for messages, because keyed by
28/// name `Db<Postgres>` and `Db<Mysql>` both stringify to `"Db"` and the
29/// second `start_watch()` would silently watch nothing. A
30/// [`Dynamic`](crate::Dynamic) instance has no usable `TypeId` — every
31/// `Dynamic<Value>` is the same type — so it carries a process-unique
32/// number instead, allocated at construction.
33#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
34pub enum WatchKey {
35    /// One watcher per configuration *type* — the attribute's contract.
36    Type(TypeId),
37    /// One watcher per [`Dynamic`](crate::Dynamic) *instance*.
38    Instance(u64),
39}
40
41/// Configurations that already have a watcher, by [`WatchKey`].
42pub(super) static STARTED: Mutex<BTreeMap<WatchKey, &'static str>> = Mutex::new(BTreeMap::new());
43
44/// Keeps a watcher alive. Dropping it stops watching.
45///
46/// The handle owns the notification backend, and the background thread owns
47/// only the receiving end. Dropping the handle closes the channel, which is
48/// what ends the thread — no flag to poll, no wake-up latency.
49///
50/// A server usually wants the watcher to outlive everything, which is what
51/// [`detach`](Self::detach) is for. Anything with a lifecycle — a test, a
52/// library, a subcommand — should hold the handle instead, so watching stops
53/// when the thing being configured goes away.
54#[must_use = "dropping the handle stops the watcher; bind it, or call `.detach()` \
55              to watch for the rest of the process"]
56pub struct WatchHandle {
57    key: WatchKey,
58    name: &'static str,
59    /// `None` only while `detach` is dismantling the handle.
60    watcher: Option<Backend>,
61}
62
63/// The two backends, kept as one owner so the handle is a single type.
64enum Backend {
65    Native(notify::RecommendedWatcher),
66    Poll(notify::PollWatcher),
67}
68
69impl WatchHandle {
70    /// Watches for the remainder of the process.
71    ///
72    /// Leaks the backend on purpose: a watcher that must never stop has no
73    /// owner to hold it, and pretending otherwise is how the handle ends up
74    /// dropped at the end of `main`'s first statement.
75    pub fn detach(mut self) {
76        if let Some(watcher) = self.watcher.take() {
77            std::mem::forget(watcher);
78        }
79
80        // The registration stays, so a later `spawn` still reports
81        // `AlreadyExists` rather than starting a second watcher.
82        std::mem::forget(self);
83    }
84
85    /// Stops watching. The same as dropping it, spelled out.
86    pub fn stop(self) {}
87
88    /// The type name this watcher was started for.
89    #[must_use]
90    pub fn name(&self) -> &'static str {
91        self.name
92    }
93}
94
95impl Drop for WatchHandle {
96    fn drop(&mut self) {
97        // `None` only mid-`detach`, which forgets the handle before this
98        // could run — but belt and braces costs one branch.
99        let Some(watcher) = self.watcher.take() else {
100            return;
101        };
102
103        // Dropping the backend closes the channel and ends the thread. Freeing
104        // the registration lets a later `spawn` start a fresh one — which is
105        // what makes this usable from tests.
106        drop(watcher);
107
108        // Recovered from poisoning rather than skipped: skipping would leak
109        // the registration forever, and the map has no invariant a panic
110        // could break — the same policy every other lock in the crate follows.
111        STARTED
112            .lock()
113            .unwrap_or_else(std::sync::PoisonError::into_inner)
114            .remove(&self.key);
115    }
116}
117
118impl std::fmt::Debug for WatchHandle {
119    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
120        f.debug_struct("WatchHandle")
121            .field("name", &self.name)
122            // The backend is a notify watcher, which has no rendering worth
123            // printing and would drown the one field that matters.
124            .finish_non_exhaustive()
125    }
126}
127
128/// Starts a background thread that runs `reload` whenever one of `files` changes.
129///
130/// Calling this twice for the same type is an error (`AlreadyExists`): a
131/// second handle could only mislead, and the first watcher keeps running.
132///
133/// `reload` is expected to swap in a new snapshot. It is handed the path
134/// whose event opened the debounce window — one path, not the set, because
135/// the window can cover several and an unbounded collection of remount
136/// directory names is what collecting them all would mean. Returning
137/// `Some(summary)` replaces the generic "reloaded" line with something more
138/// specific — which is how `diff` reports the keys that moved without
139/// logging twice.
140///
141/// Its error is reported and discarded — an invalid or half-written file must
142/// degrade to "no change", never to a crash, because the previous snapshot is
143/// still perfectly good.
144///
145/// The watch is registered *before* this function returns, so an edit that
146/// lands immediately afterwards cannot slip through the gap. Registering it on
147/// the background thread instead would leave a window — short, but reliably hit
148/// by anything that writes configuration during startup.
149///
150/// # Errors
151///
152/// If the notification backend cannot be created, if none of the directories
153/// holding `files` can be watched, or if the thread cannot be spawned. A
154/// directory that fails while others succeed is reported and skipped.
155///
156pub fn spawn(
157    key: WatchKey,
158    name: &'static str,
159    watched: Watched,
160    debounce: Duration,
161    reload: impl Fn(&Path) -> Result<Option<String>, Error> + Send + 'static,
162) -> std::io::Result<WatchHandle> {
163    spawn_with(key, name, watched, debounce, WatchMode::default(), reload)
164}
165
166/// [`spawn`], with the detection strategy chosen explicitly.
167///
168/// # Errors
169///
170/// As [`spawn`].
171///
172pub fn spawn_with(
173    key: WatchKey,
174    name: &'static str,
175    watched: Watched,
176    debounce: Duration,
177    mode: WatchMode,
178    reload: impl Fn(&Path) -> Result<Option<String>, Error> + Send + 'static,
179) -> std::io::Result<WatchHandle> {
180    // An error, not a quiet no-op handle. The old behaviour returned
181    // `Ok(handle-that-owns-nothing)`, which read as "I started watching" and
182    // was undetectable at runtime — the worst kind of success.
183    if STARTED
184        .lock()
185        .unwrap_or_else(std::sync::PoisonError::into_inner)
186        .insert(key, name)
187        .is_some()
188    {
189        return Err(std::io::Error::new(
190            std::io::ErrorKind::AlreadyExists,
191            format!(
192                "`{name}` is already being watched; hold on to the handle the \
193                 first `start_watch()` returned, or drop it before starting \
194                 another"
195            ),
196        ));
197    }
198
199    // The insertion above is what makes two concurrent `spawn` calls mutually
200    // exclusive, so it has to come first — and therefore a failure below has
201    // to undo it. Without the rollback, every later `start_watch()` for this
202    // type would find the name taken and return a success handle that owns
203    // nothing and watches nothing, silently.
204    let registered = Registered { key, armed: true };
205
206    let (sender, receiver) = mpsc::channel::<notify::Result<Event>>();
207
208    let mut backend = match mode {
209        WatchMode::Native => Backend::Native(notify::recommended_watcher(sender).map_err(to_io)?),
210        WatchMode::Poll { interval } => Backend::Poll(
211            notify::PollWatcher::new(
212                sender,
213                notify::Config::default().with_poll_interval(interval),
214            )
215            .map_err(to_io)?,
216        ),
217    };
218
219    match &mut backend {
220        Backend::Native(watcher) => watch_directories(name, watcher, &watched)?,
221        Backend::Poll(watcher) => watch_directories(name, watcher, &watched)?,
222    }
223
224    thread::Builder::new()
225        .name(format!("config-watch-{name}"))
226        .spawn(move || run(name, &watched, debounce, reload, &receiver))?;
227
228    // Everything that could fail has succeeded; from here the *handle* owns
229    // the registration and frees it on drop.
230    registered.defuse();
231
232    Ok(WatchHandle {
233        key,
234        name,
235        watcher: Some(backend),
236    })
237}
238
239/// Rolls the name registration back unless the spawn completed.
240///
241/// Every `?` between the insertion and the end of `spawn_with` — the backend,
242/// the directory watches, the thread — runs through this on the way out.
243struct Registered {
244    key: WatchKey,
245    armed: bool,
246}
247
248impl Registered {
249    /// The spawn completed; the registration now belongs to the handle.
250    fn defuse(mut self) {
251        self.armed = false;
252    }
253}
254
255impl Drop for Registered {
256    fn drop(&mut self) {
257        if self.armed {
258            STARTED
259                .lock()
260                .unwrap_or_else(std::sync::PoisonError::into_inner)
261                .remove(&self.key);
262        }
263    }
264}
265
266fn to_io(error: notify::Error) -> std::io::Error {
267    std::io::Error::new(std::io::ErrorKind::Other, error)
268}
269
270/// Watches the *directories* holding the files, not the files themselves.
271///
272/// Editors and `mv`-based atomic saves replace the inode, which silently
273/// detaches a file-level watch. Watching the parent directory survives that —
274/// and is also what makes a Kubernetes ConfigMap update, delivered as a `..data`
275/// symlink swap, visible at all.
276///
277/// Fails when nothing could be watched, rather than parking a thread on a
278/// channel that will never produce an event.
279fn watch_directories(
280    name: &'static str,
281    watcher: &mut impl Watcher,
282    watched: &Watched,
283) -> std::io::Result<()> {
284    let mut directories = Vec::<PathBuf>::new();
285
286    {
287        let mut push = |directory: PathBuf| {
288            if !directories.contains(&directory) {
289                directories.push(directory);
290            }
291        };
292
293        for file in &watched.files {
294            push(
295                file.parent()
296                    .filter(|parent| !parent.as_os_str().is_empty())
297                    .unwrap_or_else(|| Path::new("."))
298                    .to_path_buf(),
299            );
300        }
301
302        // Every searched directory, whether or not it holds a file today: a
303        // config file appearing later is exactly the event worth catching.
304        for directory in &watched.search_directories {
305            push(directory.clone());
306        }
307    }
308
309    let mut watched = 0usize;
310    let mut last_error = None;
311
312    for directory in &directories {
313        match watcher.watch(directory, RecursiveMode::NonRecursive) {
314            Ok(()) => watched += 1,
315            Err(error) => {
316                warning!("{name}: could not watch {}: {error}", directory.display());
317                last_error = Some(error);
318            }
319        }
320    }
321
322    if watched == 0 {
323        return Err(last_error.map_or_else(
324            || {
325                std::io::Error::new(
326                    std::io::ErrorKind::NotFound,
327                    format!("{name}: no configuration file to watch"),
328                )
329            },
330            to_io,
331        ));
332    }
333
334    Ok(())
335}