confik 0.15.12

Compose configuration from multiple sources without giving up type safety
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
//! Hot-reloadable configuration support.
//!
//! This module provides [`ReloadingConfig`], which wraps a configuration value
//! and allows it to be atomically reloaded at runtime.
//!
//! # Examples
//!
//! ```rust
//! # #[cfg(feature = "toml")]
//! # {
//! use confik::{Configuration, ReloadableConfig, TomlSource};
//!
//! #[derive(Debug, Configuration)]
//! struct AppConfig {
//!     port: u16,
//!     host: String,
//! }
//!
//! impl ReloadableConfig for AppConfig {
//!     type Error = confik::Error;
//!
//!     fn build() -> Result<Self, Self::Error> {
//!         Self::builder()
//!             .override_with(TomlSource::new(r#"port = 8080
//! host = "localhost""#))
//!             .try_build()
//!     }
//! }
//!
//! // Create a reloading config (no turbofish needed!)
//! let config = AppConfig::reloading().unwrap();
//!
//! // Access the current config
//! let current = config.load();
//! assert_eq!(current.port, 8080);
//!
//! // Reload from sources
//! config.reload().unwrap();
//! # }
//! ```

use std::sync::Arc;

use arc_swap::ArcSwap;

/// Trait for invoking reload callbacks.
///
/// This trait allows both `()` (no callback) and `Fn()` types to be used
/// as the callback type in `ReloadingConfig`.
pub trait ReloadCallback {
    /// Invokes the callback, if any.
    fn invoke(&self);
}

impl ReloadCallback for () {
    fn invoke(&self) {
        // No-op for unit type
    }
}

impl<F: Fn()> ReloadCallback for F {
    fn invoke(&self) {
        self()
    }
}

/// Defines how to create a new instance of [`ReloadingConfig`].
///
/// This trait is typically implemented for configuration types that need to support
/// hot-reloading. It specifies how to build a fresh instance of the configuration
/// from its sources.
pub trait ReloadableConfig: Sized {
    /// The error type returned when building fails.
    type Error;

    /// Defines the way to build the configuration item.
    ///
    /// This method should include all the logic needed to construct the configuration
    /// from its sources, including any required validations.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[cfg(feature = "toml")]
    /// # {
    /// # use confik::{Configuration, ReloadableConfig, TomlSource};
    /// # #[derive(Debug, Configuration)]
    /// # struct MyConfig { value: String }
    /// impl ReloadableConfig for MyConfig {
    ///     type Error = confik::Error;
    ///
    ///     fn build() -> Result<Self, Self::Error> {
    ///         Self::builder()
    ///             .override_with(TomlSource::new(r#"value = "test""#))
    ///             .try_build()
    ///     }
    /// }
    /// # }
    /// ```
    fn build() -> Result<Self, Self::Error>;

    /// Creates a new [`ReloadingConfig`] for this configuration type.
    ///
    /// This is a convenience method that avoids needing to specify type parameters.
    ///
    /// # Errors
    ///
    /// Returns an error if the initial configuration build fails.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[cfg(feature = "toml")]
    /// # {
    /// # use confik::{Configuration, ReloadableConfig, TomlSource};
    /// # #[derive(Debug, Configuration)]
    /// # struct MyConfig { value: String }
    /// # impl ReloadableConfig for MyConfig {
    /// #     type Error = confik::Error;
    /// #     fn build() -> Result<Self, Self::Error> {
    /// #         Self::builder().override_with(TomlSource::new(r#"value = "test""#)).try_build()
    /// #     }
    /// # }
    /// // Much cleaner than ReloadingConfig::<MyConfig, _>::build()
    /// let config = MyConfig::reloading().unwrap();
    /// # }
    /// ```
    fn reloading() -> Result<ReloadingConfig<Self, ()>, Self::Error> {
        ReloadingConfig::build()
    }
}

/// An instance of config that may reload itself.
///
/// This struct wraps a configuration value and allows it to be atomically swapped
/// with a newly-loaded version. Cloning this object is cheap as it only clones
/// the underlying `Arc` pointers.
///
/// # Type Parameters
///
/// * `T` - The configuration type that implements [`ReloadableConfig`]
/// * `F` - The type of the callback invoked after successful reloads (defaults to `()`), see [`ReloadCallback`]
#[derive(Debug)]
pub struct ReloadingConfig<T, F> {
    config: Arc<ArcSwap<T>>,
    on_update: F,
}

impl<T, F> Clone for ReloadingConfig<T, F>
where
    F: Clone,
{
    fn clone(&self) -> Self {
        ReloadingConfig {
            config: Arc::clone(&self.config),
            on_update: self.on_update.clone(),
        }
    }
}

impl<T> ReloadingConfig<T, ()>
where
    T: ReloadableConfig,
{
    /// Creates a new [`ReloadingConfig`] by building the initial configuration.
    ///
    /// # Errors
    ///
    /// Returns an error if the initial configuration build fails.
    pub fn build() -> Result<Self, <T as ReloadableConfig>::Error> {
        Ok(ReloadingConfig {
            config: Arc::new(ArcSwap::new(Arc::new(T::build()?))),
            on_update: (),
        })
    }
}

impl<T, F> ReloadingConfig<T, F> {
    /// Replaces the update callback with a new one.
    ///
    /// See [`ReloadCallback`].
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[cfg(feature = "toml")]
    /// # {
    /// # use confik::{Configuration, ReloadableConfig, ReloadingConfig, TomlSource};
    /// # #[derive(Debug, Configuration)]
    /// # struct MyConfig { value: String }
    /// # impl ReloadableConfig for MyConfig {
    /// #     type Error = confik::Error;
    /// #     fn build() -> Result<Self, Self::Error> {
    /// #         Self::builder().override_with(TomlSource::new(r#"value = "test""#)).try_build()
    /// #     }
    /// # }
    /// let config = ReloadingConfig::<MyConfig, _>::build().unwrap()
    ///     .with_on_update(|| println!("Config reloaded!"));
    /// # }
    /// ```
    #[must_use]
    pub fn with_on_update<U>(self, new: U) -> ReloadingConfig<T, U> {
        ReloadingConfig {
            config: self.config,
            on_update: new,
        }
    }

    /// Loads the current configuration.
    ///
    /// Returns an `Arc` to the current configuration value. This is a cheap operation
    /// that doesn't block writers.
    #[must_use]
    pub fn load(&self) -> Arc<T> {
        self.config.load_full()
    }
}

impl<T, F> ReloadingConfig<T, F>
where
    T: ReloadableConfig,
    F: ReloadCallback,
{
    /// Attempts to reload the configuration.
    ///
    /// On success, calls the stored update function (if any).
    /// On error, leaves the configuration unchanged.
    ///
    /// # Errors
    ///
    /// Returns an error if building the new configuration fails. In this case,
    /// the current configuration remains unchanged and the update callback is
    /// not invoked.
    pub fn reload(&self) -> Result<(), <T as ReloadableConfig>::Error> {
        let config = T::build()?;
        self.config.store(Arc::new(config));
        self.on_update.invoke();
        Ok(())
    }
}

#[cfg(feature = "signal")]
impl<T, F> ReloadingConfig<T, F>
where
    T: ReloadableConfig + Send + Sync + 'static,
    F: ReloadCallback + Clone + Send + Sync + 'static,
{
    /// Sets a listener for SIGHUP.
    ///
    /// This spawns a thread and listens for a signal using the [`signal_hook`] crate,
    /// with all of that crate's caveats. If you're setting signals already, you may wish to
    /// configure this yourself.
    ///
    /// When a SIGHUP signal is received, the configuration will be reloaded. If the reload
    /// fails and the `tracing` feature is enabled, an error will be logged.
    ///
    /// # Errors
    ///
    /// Returns an error if signal registration fails.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// # #[cfg(all(feature = "signal", feature = "toml"))]
    /// # {
    /// # use confik::{Configuration, ReloadableConfig, ReloadingConfig, TomlSource};
    /// # #[derive(Debug, Configuration)]
    /// # struct MyConfig { value: String }
    /// # impl ReloadableConfig for MyConfig {
    /// #     type Error = confik::Error;
    /// #     fn build() -> Result<Self, Self::Error> {
    /// #         Self::builder().override_with(TomlSource::new(r#"value = "test""#)).try_build()
    /// #     }
    /// # }
    /// let config = ReloadingConfig::<MyConfig, _>::build().unwrap();
    ///
    /// // Set up signal handler
    /// let handle = config.spawn_signal_handler().unwrap();
    ///
    /// // The config will now reload when receiving SIGHUP
    /// // handle.join().unwrap(); // Wait for the signal handler thread
    /// # }
    /// ```
    pub fn spawn_signal_handler(&self) -> Result<std::thread::JoinHandle<()>, std::io::Error>
    where
        <T as ReloadableConfig>::Error: std::fmt::Display,
    {
        use signal_hook::{consts::SIGHUP, iterator::Signals};

        let mut signals = Signals::new([SIGHUP])?;
        let config = self.clone();
        Ok(std::thread::spawn(move || {
            for signal in &mut signals {
                if signal == SIGHUP {
                    if let Err(err) = config.reload() {
                        #[cfg(feature = "tracing")]
                        tracing::error!(%err, "Failed to reload configuration");

                        #[cfg(not(feature = "tracing"))]
                        {
                            // Avoid unused variable warning
                            let _ = err;
                        }
                    }
                }
            }
        }))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Configuration;

    #[derive(Debug, Clone, PartialEq, Configuration)]
    struct TestConfig {
        value: u32,
    }

    impl ReloadableConfig for TestConfig {
        type Error = &'static str;

        fn build() -> Result<Self, Self::Error> {
            Ok(TestConfig { value: 42 })
        }
    }

    #[test]
    fn test_build_and_load() {
        let config = ReloadingConfig::<TestConfig, _>::build().unwrap();
        let current = config.load();
        assert_eq!(current.value, 42);
    }

    #[test]
    fn test_reload_without_callback() {
        let config = TestConfig::reloading().unwrap();
        config.reload().unwrap();
        let current = config.load();
        assert_eq!(current.value, 42);
    }

    #[test]
    fn test_reload_with_callback() {
        use std::sync::atomic::{AtomicBool, Ordering};

        let called = Arc::new(AtomicBool::new(false));
        let called_clone = Arc::clone(&called);

        let config = TestConfig::reloading().unwrap().with_on_update(move || {
            called_clone.store(true, Ordering::SeqCst);
        });

        assert!(!called.load(Ordering::SeqCst));
        config.reload().unwrap();
        assert!(called.load(Ordering::SeqCst));
    }

    #[test]
    fn test_reload_updates_all_clones() {
        use std::sync::atomic::{AtomicU32, Ordering};

        static COUNTER: AtomicU32 = AtomicU32::new(0);

        #[derive(Debug, serde::Deserialize, Configuration)]
        struct CountingConfig {
            id: u32,
        }

        impl ReloadableConfig for CountingConfig {
            type Error = std::convert::Infallible;

            fn build() -> Result<Self, Self::Error> {
                Ok(CountingConfig {
                    id: COUNTER.fetch_add(1, Ordering::SeqCst),
                })
            }
        }

        let config1 = CountingConfig::reloading().unwrap();
        let config2 = config1.clone();

        assert_eq!(config1.load().id, 0);
        assert_eq!(config2.load().id, 0);

        config1.reload().unwrap();

        assert_eq!(config1.load().id, 1);
        assert_eq!(config2.load().id, 1);
    }

    #[test]
    fn test_reload_error_leaves_config_unchanged() {
        use std::sync::atomic::{AtomicBool, Ordering};

        static SHOULD_FAIL: AtomicBool = AtomicBool::new(false);

        #[derive(Debug, serde::Deserialize, Configuration)]
        struct FallibleConfig {
            value: u32,
        }

        impl ReloadableConfig for FallibleConfig {
            type Error = &'static str;

            fn build() -> Result<Self, Self::Error> {
                if SHOULD_FAIL.load(Ordering::SeqCst) {
                    Err("Build failed")
                } else {
                    Ok(FallibleConfig { value: 42 })
                }
            }
        }

        let config = FallibleConfig::reloading().unwrap();
        assert_eq!(config.load().value, 42);

        // Make the next build fail
        SHOULD_FAIL.store(true, Ordering::SeqCst);

        // Reload should fail and leave config unchanged
        let result = config.reload();
        assert!(result.is_err());
        assert_eq!(config.load().value, 42); // Still the old value

        // Make build succeed again
        SHOULD_FAIL.store(false, Ordering::SeqCst);
        config.reload().unwrap();
        assert_eq!(config.load().value, 42);
    }

    #[test]
    fn test_callback_not_invoked_on_reload_error() {
        use std::sync::atomic::{AtomicBool, Ordering};

        static SHOULD_FAIL: AtomicBool = AtomicBool::new(false);

        #[derive(Debug, serde::Deserialize, Configuration)]
        struct FallibleConfig {
            value: u32,
        }

        impl ReloadableConfig for FallibleConfig {
            type Error = &'static str;

            fn build() -> Result<Self, Self::Error> {
                if SHOULD_FAIL.load(Ordering::SeqCst) {
                    Err("Build failed")
                } else {
                    Ok(FallibleConfig { value: 100 })
                }
            }
        }

        let callback_called = Arc::new(AtomicBool::new(false));
        let callback_called_clone = Arc::clone(&callback_called);

        let config = FallibleConfig::reloading()
            .unwrap()
            .with_on_update(move || {
                callback_called_clone.store(true, Ordering::SeqCst);
            });

        // Initial value should be 100
        assert_eq!(config.load().value, 100);

        // Successful reload should call callback
        config.reload().unwrap();
        assert!(callback_called.load(Ordering::SeqCst));

        // Reset flag
        callback_called.store(false, Ordering::SeqCst);

        // Make next build fail
        SHOULD_FAIL.store(true, Ordering::SeqCst);

        // Failed reload should NOT call callback
        let result = config.reload();
        assert!(result.is_err());
        assert!(!callback_called.load(Ordering::SeqCst));
    }
}