ek-core 0.3.0

Business-agnostic plugin host and actor-over-bus runtime on top of ek-event: capability index with runtime service discovery, per-plugin actor workers, dynamic add/remove with lifecycle broadcasts, one-shot ask/ask_service with contract gates, panic quarantine, five-step close.
Documentation
//! `Plugin` trait —— 插件实现的唯一契约(业务与宿主的边界)。
//!
//! 纪律(写进 doc,实施由宿主强制):
//! - 收:只有 [`Plugin::on_mail`] 一个入口;订阅面由 [`Plugin::routes`] 静态声明,
//!   宿主 start() 时统一装配 forwarder(v0.1 不支持运行期动态订阅)。
//! - 发:只有 [`PluginContext`](crate::PluginContext) 三个出口(put/ask/notify);
//!   业务日志属 put 的用例(0.3.0:宿主零日志,无 write_log 出口)。
//! - 状态:插件状态放对象内部自锁(`Mutex/RwLock`);on_mail 在插件 worker 线程串行执行。
//! - 停:on_stop 之后不再收到任何邮件(close 五步第②步)。

use crate::context::PluginContext;
use crate::service::ServiceDesc;
use std::any::Any;
use std::borrow::Cow;
use std::time::Duration;

use ek_event::Event;

/// 默认邮箱容量(D5,confirms:按插件覆写override)。
pub const MAILBOX_CAP: usize = 1024;

/// 连续 panic 熔断阈值(D3)。
pub const PANIC_LIMIT: u32 = 3;

/// 插件名规则(D2):首字母字母,其余 alnum/_/-,长度 1..=32。
pub fn valid_plugin_name(name: &str) -> bool {
    let b = name.as_bytes();
    if b.is_empty() || b.len() > 32 {
        return false;
    }
    if !b[0].is_ascii_alphabetic() {
        return false;
    }
    b[1..]
        .iter()
        .all(|c| c.is_ascii_alphanumeric() || *c == b'_' || *c == b'-')
}

pub trait Plugin: Any + Send + Sync {
    /// 注册键(D2 校验)。
    fn name(&self) -> &str;

    /// D1 显式依赖清单(宿主注册期校验存在性,start 期拓扑排序)。
    fn deps(&self) -> &[&str] {
        &[]
    }

    /// 订阅的 type 集合:广播 type(如业务常量 "eTick.")、
    /// 自身的寻址 type(e_req_type(本名)/e_to_type(本名))。
    /// 宿主 start() 装配 forwarder → 邮箱 → on_mail。
    fn routes(&self) -> Vec<Cow<'static, str>> {
        Vec::new()
    }

    /// 邮箱容量覆写(D5:按插件可小可大,默认 [`MAILBOX_CAP`])。
    fn mailbox_cap(&self) -> usize {
        MAILBOX_CAP
    }

    /// 插件版本(约定填 crate 版本)。默认空串 = 「未知版本」。
    ///
    /// ⚠ 陷阱(审核 F6):默认实现无法用 `env!(CARGO_PKG_VERSION)`
    /// —— 该宏在 ek-core 定义处展开,填出的是 ek-core 的版本。
    /// 插件侧必须显式覆写:
    /// ```ignore
    /// fn version(&self) -> Cow<'static, str> {
    ///     Cow::Borrowed(env!("CARGO_PKG_VERSION"))
    /// }
    /// ```
    fn version(&self) -> Cow<'static, str> {
        Cow::Borrowed("")
    }

    /// 服务目录自报(纯声明):只在 add_plugin 时被宿主读取一次抽 `id` 织入路由索引;
    /// 之后 ctx.services_of/find_service/ask_service 需要明细时由宿主**现场**调用本方法取。
    ///
    /// 铁律(审核 F8):本方法必须纯/静态/无锁/不阻塞 —— 它会在宿主管理线程上被
    /// 与插件 worker 并发调用;违反此纪律后果自负。
    ///
    /// 目录改版铁律(执行期 DV4 发现):插件目录随配置/状态变化时,必须
    /// **先更新本方法返回的内容,再 ctx.publish_services(...) 申报** ——
    /// 宿主现场取,先申报后换内容会造成 live 细节与索引脱节。
    fn services(&self) -> Vec<ServiceDesc> {
        Vec::new()
    }

    /// 可选:启动前纯接线(缓存 ctx、读配置)。禁止在此 put/注册 handler(规范)。
    fn on_init(&self, _ctx: &PluginContext) {}

    /// start() 按依赖拓扑序调用(只一次)。
    fn on_start(&self) {}

    /// close/remove 按逆依赖序调用;调用后保证不再收到邮件。
    fn on_stop(&self) {}

    /// ★ 唯一业务入口:永远在本插件 worker 线程上串行执行。
    /// 广播事件、寻址请求(eReq.<本名>)、定向通知(eTo.<本名>)都从这里进。
    fn on_mail(&self, event: &Event);
}

/// close() 的 join 等待上限(D4:超时告警不死等)。
pub const CLOSE_JOIN_TIMEOUT: Duration = Duration::from_secs(2);