Skip to main content

ConfigWatcher

Struct ConfigWatcher 

Source
pub struct ConfigWatcher<T> { /* private fields */ }
Expand description

A handle to a configuration that is automatically reloaded when any of its source files change.

  • get returns the current snapshot without locking, cheap enough to call on every request;
  • when a reload fails, the previous configuration stays active and the error is reported through on_error;
  • dropping the watcher stops the background thread.

Created by ConfigBuilder::watch.

Implementations§

Source§

impl<T> ConfigWatcher<T>

Source

pub fn get(&self) -> Arc<T>

Returns the currently active configuration snapshot.

Lock-free (backed by arc-swap), so it can be called freely on hot paths and from any thread. The returned Arc stays valid even if the configuration is swapped out in the meantime.

Source

pub fn on_change(&self, callback: impl Fn(Arc<T>) + Send + Sync + 'static)

Registers a callback invoked after each successful reload with the new configuration.

Callbacks may be registered multiple times and run on the background watcher thread — keep them short and non-blocking. A callback that panics will terminate that thread and stop hot reloading.

Source

pub fn on_error(&self, callback: impl Fn(&Error) + Send + Sync + 'static)

Registers a callback invoked when a reload fails.

The previous configuration remains active in that case. Runs on the background watcher thread, same caveats as on_change.

Trait Implementations§

Source§

impl<T> Drop for ConfigWatcher<T>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for ConfigWatcher<T>

§

impl<T> !UnwindSafe for ConfigWatcher<T>

§

impl<T> Freeze for ConfigWatcher<T>

§

impl<T> Send for ConfigWatcher<T>
where T: Sync + Send,

§

impl<T> Sync for ConfigWatcher<T>
where T: Sync + Send,

§

impl<T> Unpin for ConfigWatcher<T>

§

impl<T> UnsafeUnpin for ConfigWatcher<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.