pub struct ConfigWatcher { /* private fields */ }Expand description
配置热重载观察器
在后台定时轮询配置文件修改时间,检测到变化时自动重新加载配置。
对齐 PHP think-swoole 的热重载机制,无需重启服务即可更新配置。
§设计
- 使用
Arc<RwLock<AppConfig\>>共享配置,读无锁、写互斥 - 轮询间隔默认 5 秒(可配置),通过
tokio::time::interval实现 - 比较文件修改时间(mtime),避免频繁的文件读取
- 配置重载失败时保留旧配置,记录错误日志
§用法
ⓘ
use sz_rust_core::config::{AppConfig, ConfigWatcher};
use std::sync::Arc;
use parking_lot::RwLock;
let config = AppConfig::load_from_dir("config/").unwrap();
let shared = Arc::new(RwLock::new(config));
let watcher = ConfigWatcher::new("config/", shared.clone());
// 启动后台监听(spawn 到 tokio runtime)
let handle = watcher.start();
// 读取最新配置(热重载后自动生效)
let current = shared.read().clone();
// 停止监听
handle.stop();Implementations§
Source§impl ConfigWatcher
impl ConfigWatcher
Sourcepub fn with_poll_interval(self, secs: u64) -> Self
pub fn with_poll_interval(self, secs: u64) -> Self
设置轮询间隔(秒)
Sourcepub fn start(self) -> ConfigWatcherHandle
pub fn start(self) -> ConfigWatcherHandle
启动后台配置监听
返回 ConfigWatcherHandle,调用 stop() 可停止监听。
Auto Trait Implementations§
impl !Freeze for ConfigWatcher
impl !RefUnwindSafe for ConfigWatcher
impl !UnwindSafe for ConfigWatcher
impl Send for ConfigWatcher
impl Sync for ConfigWatcher
impl Unpin for ConfigWatcher
impl UnsafeUnpin for ConfigWatcher
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.