Skip to main content

PluginManager

Struct PluginManager 

Source
pub struct PluginManager { /* private fields */ }
Expand description

插件管理器:负责注册、拓扑排序启动、严格逆序关闭(含启动失败回滚)

内置生命周期状态机(PluginState):跟踪每个插件的状态, 非法操作(重复启动、恢复未降级插件等)被状态机拒绝并记录日志; 额外提供 degrade_all / resume_all / fault / reset 生命周期管理。

§示例

ⓘ
let mgr = PluginManager::new()
    .add(db_plugin)
    .add(cache_plugin);
mgr.start_all().await?;
// ... 运行应用 ...
mgr.stop_all().await;

Implementations§

Source§

impl PluginManager

Source

pub fn new() -> Self

创建空的插件管理器

Source

pub fn add<P: Plugin + 'static>(self, plugin: P) -> Self

手动注册插件(链式调用)

§示例
ⓘ
PluginManager::new()
    .add(DbPlugin::new(db_config))
    .add(CachePlugin::new(cache_config))
Source

pub fn add_discovered(self, plugins: Vec<Arc<dyn Plugin>>) -> Self

编译期自动发现的插件批量注册

与 #[plugin] 宏配合使用,将 linkme 收集到的插件批量加入管理器。

Source

pub async fn start_all(&self) -> Result<()>

拓扑排序后依次启动所有插件,并记录实际启动顺序

若任一插件启动失败,立即按逆序回滚本轮已成功启动的插件,避免系统停留在部分启动状态; 启动失败的插件标记为 Faulted(state() 可查)。 已在运行的插件被状态机拒绝(跳过并告警),不会重复执行 start 钩子, 且其启动记录被保留——二次 start_all 不会让 stop_all 失去可关闭的插件。

Source

pub async fn stop_all(&self)

严格按“实际成功启动顺序”的逆序关闭插件(后启动的先关闭)

只关闭记录已启动的插件;未调用过 start_all 时回退到注册列表逆序; 已停止(含启动失败回滚)后重复调用为空操作,不会重复执行 stop 钩子。 即使某个插件关闭失败,也会继续关闭其余插件(仅记录错误日志)。

Source

pub fn state(&self, name: &str) -> Option<PluginState>

查询插件当前生命周期状态(未知插件返回 None)

Source

pub async fn degrade_all(&self)

降级所有运行中的插件(按启动逆序:依赖方先降级,与 stop_all 同理)

非 Running 状态的插件跳过;降级钩子失败的插件自动故障转移(Faulted + on_fault)。

Source

pub async fn resume_all(&self)

恢复所有已降级的插件(按启动顺序:被依赖方先就绪)

非 Degraded 状态的插件跳过;恢复钩子失败的插件自动故障转移。

Source

pub async fn fault(&self, name: &str) -> bool

手动将指定插件转入故障状态并执行 on_fault 清理钩子

返回 false 表示插件未知或当前状态不可故障转移(如未启动); 对已 Faulted 的插件幂等返回 true。

Source

pub fn reset(&self, name: &str) -> bool

将故障插件重置为 Stopped,允许重新走启动流程

返回 false 表示插件未知或状态机拒绝(非 Faulted 不可重置)。

Source

pub fn check_duplicate_names(&self) -> Result<(), String>

确保插件之间没有 name 冲突

Trait Implementations§

Source§

impl Default for PluginManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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