Skip to main content

ek_core/
service.rs

1//! 服务声明与解析(0.2.0 新增):插件自报目录、宿主持【纯路由索引】。
2
3use crate::main_engine::PluginState;
4use std::any::{Any, type_name};
5use std::time::Duration;
6
7/// 一条服务声明(住回插件本身;宿主仅在 add 时抽 `id` 织入路由索引)。
8#[derive(Clone, Debug, PartialEq, Eq, Default)]
9pub struct ServiceDesc {
10    /// 服务标识,如 `order.place`(允许多提供方共享同一 id)。
11    pub id: String,
12    /// 请求类型名(type_name::<Req>() 自填)。
13    pub req_type: String,
14    /// 返回类型名(type_name::<Res>() 自填)。
15    pub res_type: String,
16    /// 人读说明。
17    pub doc: String,
18    /// 建议超时(可选)。
19    pub timeout_hint: Option<Duration>,
20}
21
22impl ServiceDesc {
23    /// 类型化构造:从 Req/Res 具体类型抓取 type_name。
24    pub fn describe<Req, Res>(id: impl Into<String>, doc: impl Into<String>) -> Self
25    where
26        Req: Any + Send + Sync,
27        Res: Any + Send + Sync,
28    {
29        Self {
30            id: id.into(),
31            req_type: type_name::<Req>().to_string(),
32            res_type: type_name::<Res>().to_string(),
33            doc: doc.into(),
34            timeout_hint: None,
35        }
36    }
37
38    /// 附带建议超时。
39    pub fn with_timeout(mut self, d: Duration) -> Self {
40        self.timeout_hint = Some(d);
41        self
42    }
43}
44
45/// `find_service` 的一条解析结果:索引命中 + 现场明细。
46#[derive(Clone, Debug, PartialEq)]
47pub struct ProviderRef {
48    /// 提供方注册名(ask/ask_service 路由即以此为 target)。
49    pub name: String,
50    /// 提供方申报版本(空串 = 未声明版本,被动跟随策略不主动迁移)。
51    pub version: String,
52    /// 提供方当前状态(Ready 亦可被发现,但 ask_service 只路由 Running 者)。
53    pub state: PluginState,
54    /// 该服务的完整描述(现场取,永远最新)。
55    pub service: ServiceDesc,
56}
57
58/// 生命周期事件的固定键载荷(订阅 `ePlugUp`/`ePlugDown` 这类不带后缀的固定 type;
59/// 另有 `ePlugUp.<名>` 后缀变体,载荷为版本字符串)。
60#[derive(Clone, Debug, PartialEq, Eq)]
61pub struct PlugNotice {
62    pub name: String,
63    pub version: String,
64}