Skip to main content

ReloadingConfig

Struct ReloadingConfig 

Source
pub struct ReloadingConfig<T, F> { /* private fields */ }
Available on crate feature reloading only.
Expand description

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

Implementations§

Source§

impl<T> ReloadingConfig<T, ()>

Source

pub fn build() -> Result<Self, <T as ReloadableConfig>::Error>

Creates a new ReloadingConfig by building the initial configuration.

§Errors

Returns an error if the initial configuration build fails.

Source§

impl<T, F> ReloadingConfig<T, F>

Source

pub fn with_on_update<U>(self, new: U) -> ReloadingConfig<T, U>

Replaces the update callback with a new one.

See ReloadCallback.

§Examples
let config = ReloadingConfig::<MyConfig, _>::build().unwrap()
    .with_on_update(|| println!("Config reloaded!"));
Source

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

Loads the current configuration.

Returns an Arc to the current configuration value. This is a cheap operation that doesn’t block writers.

Source§

impl<T, F> ReloadingConfig<T, F>

Source

pub fn reload(&self) -> Result<(), <T as ReloadableConfig>::Error>

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.

Source§

impl<T, F> ReloadingConfig<T, F>
where T: ReloadableConfig + Send + Sync + 'static, F: ReloadCallback + Clone + Send + Sync + 'static,

Source

pub fn spawn_signal_handler(&self) -> Result<JoinHandle<()>, Error>

Available on crate feature signal only.

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
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

Trait Implementations§

Source§

impl<T, F> Clone for ReloadingConfig<T, F>
where F: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, F: Debug> Debug for ReloadingConfig<T, F>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, F> Freeze for ReloadingConfig<T, F>
where F: Freeze,

§

impl<T, F> RefUnwindSafe for ReloadingConfig<T, F>

§

impl<T, F> Send for ReloadingConfig<T, F>
where F: Send, T: Sync + Send,

§

impl<T, F> Sync for ReloadingConfig<T, F>
where F: Sync, T: Sync + Send,

§

impl<T, F> Unpin for ReloadingConfig<T, F>
where F: Unpin,

§

impl<T, F> UnsafeUnpin for ReloadingConfig<T, F>
where F: UnsafeUnpin,

§

impl<T, F> UnwindSafe for ReloadingConfig<T, F>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more