moonbit-beam-node

    Erlang 节点高层连接管理(重连退避/心跳/多节点表/连接状态机;核心零依赖,状态/迁移数据化)

    erlang
    node
    reconnect
    state-machine
    moonbit
    Download zip
    Author
    Version
    0.0.1
    License
    Apache-2.0
    Last updated
    13 hours ago
    Downloads
    3

    #moonbit-beam-node

    Erlang 节点高层连接管理(MoonBit 纯核):重连(指数退避)、心跳位、多节点 表、连接状态机。语义自 OTP26 net_kernel(up_nodedown 本地判定/dist_auto_connect 策略)+moonactor/erl_dist 真件(NodeEvents/NodeSupervisor/NodeRetry——门 C 四层真连通)抽取重构;clean-room 零拷贝。

    核心包零依赖:连接执行(EPMD+握手)藏 ConnectPort trait 后(六边形—— 消费方组装 beam-dist+beam-epmd);状态机迁移表数据化显式穷尽(大 gen 思想——gen 化=搬数据)。

    #包结构

    包内容
    thy1016/moonbit-beam-node状态机+NodeRetry+NodeManager 组合根(唯一包)

    状态:0.0.1 骨架(阶段 4)。依赖面:beam-dist(握手)/beam-epmd(端口 发现)——组合根由消费方装配(双实现安放律)。

    #特性

    • 状态机:Disconnected→Connecting→Connected 全迁移表数据化(显式 穷尽+非法迁移诚实拒 IllegalTransition)。
    • 重连退避:NodeRetry 指数(base<<tries)+cap 封顶+max 停机哨兵 -1 (A10 同源;罐守恒三断言)。
    • nodedown 本地判定:conn_lost 事件→Disconnected→reconnect 决策 (net_kernel up_nodedown 对位;不依赖对端 EXIT 帧)。
    • ConnectPort 注入:连接执行位 trait(恒定真/假实现罐形)。

    #测试门

    moon check --deny-warn && moon test --target native

    #演进位

    • 多节点表(mesh TOML——moonkind/dist D-3 形)+心跳/watch 常驻任务位
    • 迁移表 gen 化(TOML→表驱动,契约 〇)
    • 真网门(beam-dist 组装真连——归役自跑,律 11)

    #仓库

    • mooncakes 名: thy1016/moonbit-beam-node
    • 仓库: git@gitlink.org.cn:thy7/mbt.beam.node.git(master)
    • 孵化: bitbang 主仓孵化件独立入库(2026-11-06;孵化期零跨仓 import——组合根候 mooncakes 发版后 moon add 接线)

    ConnectPort

    pub(open) trait ConnectPort {
    async fn connect(self : Self, node : String) -> Bool
    }

    连接执行位(六边形:EPMD+握手重放由消费方注入——beam-dist 组装)

    NodeEvents

    pub(open) trait NodeEvents {
    fn on_nodedown(self : Self, node : String) -> Unit
    }

    节点事件订阅面(门 C 第三监督者接线口——net_kernel:monitor_nodes 对应形; 连接 FIN/读错→on_conn_lost→订阅者收 nodedown——本地判定语义)

    TickPort

    pub(open) trait TickPort {
    fn now_ms(self : Self) -> Int64
    }

    时钟口(六边形注入——罐=Array 单元闭包假时钟:推进可见+多消费者共享 同一单元即同步推进;生产=单调毫秒钟)

    NodeError

    pub(all) suberror NodeError {
    UnknownState(String)
    IllegalTransition(String)
    }

    AutoConnect

    pub(all) enum AutoConnect {
    Once
    Never
    }

    自动重连策略(net_kernel dist_auto_connect 对位)

    HeartVerdict

    pub(all) enum HeartVerdict {
    Alive
    Stale
    Dead
    }

    活性判定三窗 verdict

    Heartbeat

    pub struct Heartbeat {
    policy : HeartbeatPolicy
    tick : &TickPort
    // private fields
    }

    心跳看门(单节点账——Manager 组装面;last_seen 初值=构造时刻)

    Heartbeat::beat

    fn Heartbeat::beat(self : Heartbeat) -> Unit

    对端心跳到达(活性刷新——last_seen=now;节拍守恒面)

    Heartbeat::new

    fn Heartbeat::new(policy? : HeartbeatPolicy, tick~ : &TickPort) -> Heartbeat

    Heartbeat::poll

    fn Heartbeat::poll(self : Heartbeat) -> HeartVerdict

    活性推演(纯读——三窗判定;elapsed=now-last_seen 单调差)

    HeartbeatPolicy

    pub(all) struct HeartbeatPolicy {
    interval_ms : Int64
    tolerance : Int
    }

    心跳策略(interval+容错倍数——判死窗=interval×tolerance)

    HeartbeatPolicy::new

    fn HeartbeatPolicy::new(interval_ms? : Int64, tolerance? : Int) -> HeartbeatPolicy

    MonEntry

    pub(all) struct MonEntry {
    key : RefKey
    watched : String
    }

    monitor 账页(我方 = watcher 侧登记)

    NodeAccount

    pub struct NodeAccount {
    node : String
    // private fields
    }

    节点账本(单连接单账——多节点多账由 NodeRegistry 组装)

    NodeAccount::demonitor

    fn NodeAccount::demonitor(self : NodeAccount, key~ : RefKey) -> Unit

    monitor 摘账(按 ref 配对;beam-dist 分派 {20,…,ref} 后调)

    NodeAccount::exit_count

    fn NodeAccount::exit_count(self : NodeAccount) -> Int

    被监视者了断计数(直读)

    NodeAccount::lost_count

    fn NodeAccount::lost_count(self : NodeAccount) -> Int

    连接丢失计数(罐测断言用)

    NodeAccount::monitor_count

    fn NodeAccount::monitor_count(self : NodeAccount) -> Int

    monitor 账登记页数(直读)

    NodeAccount::monitored_exit

    fn NodeAccount::monitored_exit(self : NodeAccount, key~ : RefKey) -> Unit

    被监视者了断(按 ref 清账+事件计数;beam-dist 分派 {21,…,reason} 后调)

    NodeAccount::new

    fn NodeAccount::new(node? : String) -> NodeAccount

    NodeAccount::on_conn_lost

    fn NodeAccount::on_conn_lost(self : NodeAccount, events : &NodeEvents) -> Unit

    连接丢失(门 C 事件源):TCP FIN/读错→清账+lost 计数+订阅者回调—— 本地判定(不依赖对端 EXIT 帧——对端死时发不出);Erlang 语义: monitor 的该节点所有 watch 同时了断(reason=noconnection)

    NodeAccount::register

    fn NodeAccount::register(self : NodeAccount, key~ : RefKey, watched~ : String) -> Unit

    monitor 登记(watcher 侧;beam-dist 分派 {19,watcher,watched,ref} 后调)

    NodeManager

    pub struct NodeManager {
    node : String
    retry : NodeRetry
    conn : &ConnectPort
    events : &NodeEvents
    // private fields
    }

    节点管理器:状态机+退避+执行位+事件面组合(门 C 四层闭环宿主: conn_lost→账清→nodedown 事件→决策→重连执行)

    NodeManager::account

    fn NodeManager::account(self : NodeManager) -> NodeAccount

    主账(本节点连接的账本——罐测/上层读写用)

    NodeManager::heartbeat_dead

    async fn NodeManager::heartbeat_dead(self : NodeManager, hb : Heartbeat) -> Bool raise NodeError

    判死组装法(四层闭环接线:poll Dead→mark_lost 链——nodedown/决策/重连); 返回 true=判死且重连成功回 Connected;false=未判死或重连耗尽。 非 Dead 时零动作(静置/容错窗不惊链)。

    NodeManager::mark_lost

    async fn NodeManager::mark_lost(self : NodeManager) -> Bool raise NodeError

    连接丢失真形(门 C 事件源接线):状态机走 conn_lost→主账 on_conn_lost (清账+lost 计数+nodedown 事件上抛订阅者→决策)→决策为 reconnect 时 执行位重连(退避循环)——本地判定语义,不依赖对端 EXIT 帧。 返回 true=重连成功回 Connected;false=降级或重试耗尽。

    NodeManager::new

    fn NodeManager::new(node~ : String, retry~ : NodeRetry, conn~ : &ConnectPort, events~ : &NodeEvents) -> NodeManager

    NodeManager::reconnect

    async fn NodeManager::reconnect(self : NodeManager) -> Bool raise NodeError

    重连(Once 策略执行面):状态机走 connect→执行位→ack/fail→退避循环 返 true=Connected;false=重试耗尽(停机哨兵)

    NodeManager::state

    fn NodeManager::state(self : NodeManager) -> String

    NodeRegistry

    pub struct NodeRegistry {
    // private fields
    }

    节点注册表(多节点多账组装)

    NodeRegistry::lookup

    fn NodeRegistry::lookup(self : NodeRegistry, node : String) -> NodeAccount?

    按名取账(未注册返 None——get_conn 失败形对位)

    NodeRegistry::new

    NodeRegistry::on_all_lost

    fn NodeRegistry::on_all_lost(self : NodeRegistry, events : &NodeEvents) -> Unit

    全账清(节点全下线面——逐账 on_conn_lost;遍历序=注册序)

    NodeRegistry::register

    fn NodeRegistry::register(self : NodeRegistry, node : String) -> NodeAccount

    注册节点账(重名=幂等跳过——sys_dist ets insert 覆盖语义对位); 返回该节点账(新或既有)

    NodeRegistry::size

    fn NodeRegistry::size(self : NodeRegistry) -> Int

    已注册节点数

    NodeRetry

    pub(all) struct NodeRetry {
    base_ms : Int
    cap_ms : Int
    max : Int
    }

    重连退避(A10 同源:指数+cap+max 停机哨兵 -1)

    NodeRetry::delay

    fn NodeRetry::delay(self : NodeRetry, tries : Int) -> Int

    NodeRetry::new

    fn NodeRetry::new(base_ms? : Int, cap_ms? : Int, max? : Int) -> NodeRetry

    NodeState

    pub(all) enum NodeState {
    Disconnected
    Connecting
    Connected
    }

    NodeSupervisor

    pub struct NodeSupervisor {
    // private fields
    }

    节点监督者:impl NodeEvents(订阅面)——事件入账+按策略出决策

    NodeSupervisor::decisions

    fn NodeSupervisor::decisions(self : NodeSupervisor) -> Array[String]

    决策账(罐测断言用;只读——页形 "node:reconnect|degrade")

    NodeSupervisor::down_count

    fn NodeSupervisor::down_count(self : NodeSupervisor) -> Int

    nodedown 事件计数

    NodeSupervisor::new

    NodeSupervisor::on_nodedown

    fn NodeSupervisor::on_nodedown(self : NodeSupervisor, node : String) -> Unit

    nodedown 决策(常规方法真体——晋升面收口:impl 与它同名即撞名保护, 零隐式晋升告警;订阅面/罐直调走此面)

    RefKey

    pub(all) struct RefKey {
    a : Int64
    b : Int64
    c : Int64
    }

    monitor 配对句柄(不透明键——demonitor/exit 按 ref 配对摘账; 结构化三元=ERef(node 名序号/序号/creation)解码形,相等比较即配对)

    RefKey::new

    fn RefKey::new(a~ : Int64, b? : Int64, c? : Int64) -> RefKey

    Transition

    pub(all) struct Transition {
    from : NodeState
    event : String
    to : NodeState
    }

    迁移表(数据面——显式穷尽;新迁移=新行)

    state_name

    fn state_name(s : NodeState) -> String

    状态名(文本面)

    transition

    fn transition(from : NodeState, event : String) -> NodeState raise NodeError

    迁移查找(纯函数;查无=IllegalTransition 诚实拒——零静默默认臂)

    transitions

    fn transitions() -> Array[Transition]

    迁移表(数据面真源——显式穷尽;新迁移=新行;fn 形因 const Array 限原语)