pub struct ReloadingConfig<T, F> { /* private fields */ }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 implementsReloadableConfigF- The type of the callback invoked after successful reloads (defaults to()), seeReloadCallback
Implementations§
Source§impl<T> ReloadingConfig<T, ()>where
T: ReloadableConfig,
impl<T> ReloadingConfig<T, ()>where
T: ReloadableConfig,
Sourcepub fn build() -> Result<Self, <T as ReloadableConfig>::Error>
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>
impl<T, F> ReloadingConfig<T, F>
Sourcepub fn with_on_update<U>(self, new: U) -> ReloadingConfig<T, U>
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§impl<T, F> ReloadingConfig<T, F>where
T: ReloadableConfig,
F: ReloadCallback,
impl<T, F> ReloadingConfig<T, F>where
T: ReloadableConfig,
F: ReloadCallback,
Sourcepub fn reload(&self) -> Result<(), <T as ReloadableConfig>::Error>
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,
impl<T, F> ReloadingConfig<T, F>where
T: ReloadableConfig + Send + Sync + 'static,
F: ReloadCallback + Clone + Send + Sync + 'static,
Sourcepub fn spawn_signal_handler(&self) -> Result<JoinHandle<()>, Error>
Available on crate feature signal only.
pub fn spawn_signal_handler(&self) -> Result<JoinHandle<()>, Error>
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