pub struct CapabilityRegistry { /* private fields */ }Expand description
中心能力注册表,提供注册/发现/调用的统一入口。
内部使用 parking_lot::RwLock<HashMap<String, Arc<dyn Capability>>> 保证并发安全。
所有读操作(get/find/search/list)在读锁内完成 Arc 克隆后释放锁,不跨 await 点。
§调用链路
call / call_with_tenant 执行三步链路:参数校验 → 权限检查 → 能力调用。
未设置 PermissionChecker 时默认放行。
§性能指标
| 操作 | 延迟 |
|---|---|
| register | ~187 ns |
| get | ~38 ns |
| find_by_tags (1000 能力) | ~20 μs |
Implementations§
Source§impl CapabilityRegistry
impl CapabilityRegistry
pub fn new() -> Self
Sourcepub fn set_permission_checker(&self, checker: Arc<dyn PermissionChecker>)
pub fn set_permission_checker(&self, checker: Arc<dyn PermissionChecker>)
设置权限检查器,设置后所有 call / call_with_tenant 将在能力调用前执行权限检查。
pub fn register(&self, cap: Arc<dyn Capability>) -> Option<Arc<dyn Capability>>
pub fn unregister(&self, name: &str) -> Option<Arc<dyn Capability>>
pub fn get(&self, name: &str) -> Option<Arc<dyn Capability>>
pub fn search(&self, query: &str) -> Vec<Arc<dyn Capability>>
pub fn list_all(&self) -> Vec<Arc<dyn Capability>>
pub fn list_by_source( &self, source: CapabilitySource, ) -> Vec<Arc<dyn Capability>>
Sourcepub async fn call(&self, name: &str, args: Value) -> CapResult<Value>
pub async fn call(&self, name: &str, args: Value) -> CapResult<Value>
调用能力,使用默认 tenant_id = 0(无租户上下文)。
执行链路:参数校验 → 权限检查 → 能力调用。
Sourcepub async fn call_with_tenant(
&self,
name: &str,
args: Value,
tenant_id: i64,
) -> CapResult<Value>
pub async fn call_with_tenant( &self, name: &str, args: Value, tenant_id: i64, ) -> CapResult<Value>
调用能力,携带租户上下文用于权限检查。
执行链路:参数校验 → 权限检查 → 能力调用。
未设置 PermissionChecker 时默认放行。
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn metrics(&self) -> CapMetrics
pub fn list_info(&self) -> Vec<CapabilityInfo>
Trait Implementations§
Auto Trait Implementations§
impl !Freeze for CapabilityRegistry
impl !RefUnwindSafe for CapabilityRegistry
impl !UnwindSafe for CapabilityRegistry
impl Send for CapabilityRegistry
impl Sync for CapabilityRegistry
impl Unpin for CapabilityRegistry
impl UnsafeUnpin for CapabilityRegistry
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 more