Skip to main content

PluginRegistry

Struct PluginRegistry 

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

插件注册表(运行阶段使用,无锁)

由 PluginManagerBuilder::build() 生成,存储不可变的插件集合。 运行阶段通过 HashMap 直接查找,无需获取任何锁,实现 O(1) 无锁查找。 拓扑排序结果在构建时计算一次并缓存,多次调用 get_all() 无需重新计算。

§示例

ⓘ
use yang_base::plugin::{Plugin, PluginManagerBuilder};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut builder = PluginManagerBuilder::new();
    builder.register(MyPlugin).await?;
    let registry = builder.build();

    // 无锁 O(1) 查找
    if let Some(plugin) = registry.get("my_plugin") {
        println!("找到插件: {}", plugin.name());
    }

    // 获取缓存的排序结果(无需重新计算)
    let all_plugins = registry.get_all();

    // 关闭所有插件
    registry.shutdown().await?;

    Ok(())
}

Implementations§

Source§

impl PluginRegistry

Source

pub fn get(&self, name: &str) -> Option<&Arc<dyn Plugin>>

查找插件(无锁,O(1) HashMap 查找)

直接通过 HashMap 查找,无需获取任何锁。

§参数
  • name: 插件名称
§返回
  • Some(&Arc<dyn Plugin>): 找到插件
  • None: 插件不存在
§示例
ⓘ
use yang_base::plugin::PluginManagerBuilder;

let registry = PluginManagerBuilder::new().build();
if let Some(plugin) = registry.get("my_plugin") {
    println!("找到插件: {}", plugin.name());
}
Source

pub fn get_all(&self) -> &[Arc<dyn Plugin>]

获取所有已注册插件(返回缓存的排序结果引用)

直接返回构建时缓存的拓扑排序结果,无需重新计算。

§返回
  • 按依赖关系排序的插件切片引用
§示例
ⓘ
use yang_base::plugin::PluginManagerBuilder;

let registry = PluginManagerBuilder::new().build();
let plugins = registry.get_all();
for plugin in plugins {
    println!("插件: {} v{}", plugin.name(), plugin.version());
}
Source

pub fn get_config(&self, name: &str) -> Option<&JsonValue>

获取插件配置

§参数
  • name: 插件名称
§返回
  • Some(&JsonValue): 插件配置
  • None: 配置不存在
Source

pub async fn shutdown(&self) -> Result<(), BaseError>

关闭所有插件(逆序关闭)

按照拓扑排序的逆序调用每个插件的 on_shutdown 方法, 确保依赖其他插件的插件先被关闭。

§返回
  • Ok(()): 所有插件关闭成功
  • Err(BaseError): 某个插件关闭失败
§示例
ⓘ
use yang_base::plugin::PluginManagerBuilder;

let registry = PluginManagerBuilder::new().build();
// ... 使用插件 ...
registry.shutdown().await?;

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> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

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 more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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