ek-core 0.1.0

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

use crate::context::PluginContext;
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
    }

    /// 可选:启动前纯接线(缓存 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);