openclaw源码解读(6)——server.impl.ts 真正的启动引擎 第三阶段:网络栈启动(HTTP + WS)2
第四阶段:运行时激活(Bring It Alive)L1514 - EOF
├─ catch中异常处理:关闭生命周期 // L1364-1494
├─ startGatewayEarlyRuntime() // ⑬ 早期运行时L1514-1552
│ ├─ Bonjour 服务发现
│ ├─ Tailscale 集成
│ ├─ 技能自动刷新定时器
│ └─ 定期维护(去重清理、媒体清理)
│
├─ startGatewayEventSubscriptions() // ⑭ 事件订阅L1567-1579
│ ├─ Agent 运行事件
│ ├─ Chat 流式状态
│ └─ Session 生命周期
│
├─ startGatewayRuntimeServices() // ⑮ 运行时服务L1584-1589
│ └─ 通道健康监控、模型定价刷新
│
├─ createGatewayAuxHandlers() // ⑯ 辅助处理器L1624-1635
│ ├─ ExecApprovalManager(命令审批)
│ ├─ PluginApprovalManager(插件审批)
│ └─ SecretsHandlers(凭据管理)
│
├─ createGatewayRequestContext() // ⑰ 请求上下文(所有 handler 的共享状态)L1888-1977
│
├─ reloadDeferredGatewayPlugins() // ⑱ 延迟通道插件加载L2006-2017
│
├─ attachGatewayWsHandlers() // ⑲ WS 消息处理 ⭐ L2033-2060
│ ├─ 连接认证(token/device/session generation)
│ ├─ 方法路由 → gatewayMethods
│ └─ 事件广播分发
│
├─ startListening() // �� HTTP 端口真正开始接受连接 L2062
│
├─ startGatewayPostAttachRuntime() // ⑳ Post-Attach L2109-2216
│ ├─ Tailscale Funnel
│ ├─ 插件服务启动
│ ├─ startChannels() → 通道开始接收消息
│ └─ startupSidecarsReady = true
│
├─ activateGatewayScheduledServices() // ㉑ 定时任务 L2086-2097
│ ├─ Cron jobs
│ └─ Heartbeat runner
│
├─ startManagedGatewayConfigReloader() // ㉒ 配置热重载 L2236-2317
│ └─ watch config file → 变更时 reload
│
└─ return { close } // 返回关闭函数 L2393-2414
回顾一下,createGatewayRuntimeState 已经在 L1200 返回了 HTTP/WS 服务器等基础设施。接下来是把这些设施组装成可运行的 gateway。
块1 已融入到《openclaw源码解读(5)》中
块3执行有异常的话,块2部分则属于在catch中”关闭生命周期”
块2:L1364-1494 — 关闭生命周期(Close Prelude + Close Handler)。
这里看起来一大段,但全是函数定义,还没有执行。它定义了 gateway 如何优雅关机的全套流程。
关闭三部曲:
beginClosePrelude() runClosePrelude() createCloseHandler()
(L1350-1370) (L1350-1402) (L1428-1494)
│ ├─ beginClosePrelude() │
├─ 标记 draining 开始 ├─ 断开所有节点连接 ├─ 停止 Bonjour 广播
├─ gatewayInstanceDi ├─ 关闭 watch-node HTTP ├─ 关闭 Tailscale
| spatchReady = false
│ ├─ 清插件元数据缓存 ├─ 释放插件路由
├─ 关闭 gatewayInstance ├─ runGatewayClos ├─ 停止所有 channels
| ePrelude():
├─ cronReconciliation │ ├─ 停止诊断心跳 ├─ 停 cron、停心跳
│ .invalidate() │ ├─ 清除 skills 刷新定时器 ├─ 停 tick/health 定时器
├─ 清 post-ready 维护 │ ├─ 销毁限流器 ├─ 停 dedupe/media/worktree 清
| 定时器 | 理
├─ 停 retained 插件清理 │ ├─ 停定价刷新 ├─ 取消所有事件订阅
│ │ ├─ 停 channel 健康监控 ├─ 终止活动 chat runs
│ │ ├─ 停事件循环健康检查 ├─ 清理重启恢复候选
│ │ ├─ 清 secrets 快照 ├─ drainActiveSessions
│ │ └─ 关 MCP 服务器 └─ 关 HTTP/WS 服务器
│ │
└─ 停 config reloader └─ ...
设计意图:先做无损、无副作用的清理(prelude),再执行可能阻塞的停止操作(handler)。
块3 启动gateway:L1495-2389
块3-1:L1512-1557 — 早期运行时启动(Early Runtime)
try {
const earlyRuntime = await startupTrace.measure("runtime.early", () =>
loadGatewayStartupEarlyModule().then(({ startGatewayEarlyRuntime }) =>
startGatewayEarlyRuntime({...})
)
);
这里是真正开始启动的标志。 startGatewayEarlyRuntime 做的事情:
- 启动 Bonjour/mDNS 服务发现
- 注册节点发现 + 广播能力
- 初始化 media 清理定时器(如果配置了 TTL)
- 初始化 skills 刷新定时器
- 传递 chatRunState、restartRecoveryCandidates 等共享状态
返回后立即绑定:
runtimeState.bonjourStop = earlyRuntime.bonjourStop;
getActiveTaskCount = earlyRuntime.getActiveTaskCount;
runtimeState.skillsChangeUnsub = earlyRuntime.skillsChangeUnsub;
块3-2:L1559-1591 — 运行时订阅 + 服务
startGatewayEventSubscriptions → runtimeSubscriptions
// 注册所有内部事件监听:
// - session 创建/销毁
// - agent run 序号递增
// - chat run 生命周期
// - tool event 转发
// - 重启恢复候选管理
startGatewayRuntimeServices → runtimeServices
// 启动后台服务(定时任务、健康检查等)
Object.assign(runtimeState, runtimeSubscriptions);
Object.assign(runtimeState, runtimeServices);
两个 Object.assign 把订阅和服务的结果合并到 runtimeState,所以 runtimeState 是逐步组装起来的。
块3-3:L1593-2206 — Gateway 处理器(Handlers)这是核心部分:
// 1. 操作员审批系统 L1601-1610
const approvalSessionEvents = createOperatorApprovalSessionEventRuntime({...});
// 2. 创建三大审批管理器,处理 exec 审批、插件审批、system-agent 审批的 WebSocket 事件推送 L1612-1639
const {
execApprovalManager, // exec 命令审批
pluginApprovalManager, // 插件审批
systemAgentApprovalManager, // 系统 agent 审批
extraHandlers, // 额外的网关方法处理器(辅助方法)
coreGatewayHandlers, // 核心网关方法(来自 server-methods.js)
} = ...
// 3. 注册审批重放 L1640-1642
approvalManagersForReplay.set("exec", execApprovalManager);
approvalManagersForReplay.set("plugin", pluginApprovalManager);
approvalManagersForReplay.set("system-agent", systemAgentApprovalManager);
// 4. 撤销会话附加的权限,同时对处于pending状态的,需要pluginApprovalManager和execApprovalManager审批的记录,都设为超时。L1643-1658
// 5. 组装网关方法注册表 buildAttachedGatewayMethodRegistry(pluginRegistry) ,这个注册表决定了 gateway 对外暴露哪些 RPC 方法(sessions.create、channels.start 等),后续 WebSocket 客户端就是通过这个注册表找到对应处理器的。L1664-1693 // 合并三类处理器:
// ① 注册核心方法(coreGatewayHandlers)
// ③ 辅助方法(extraHandlers),按 isCoreGatewayMethodClassified 分流 L1669-1675。 所有的辅助方法(extraHandlers),都注册到coreDescriptorHandlers[method](是gateway核心方法),和auxHandlers[method](非核心方法)L1667-1675
coreDescriptorHandlers -> filter过滤 -> 构建核心方法描述器(coreDescriptors) L1676-1683
coreDescriptors及其他描述器 -> 注册gateway方法并返回
// ② 注册插件方法(pluginRegistry.gatewayHandlers)L1705-1726
总结当下的阶段性结构:
第三阶段(网络栈) 第四阶段(运行时组装)
┌─────────────────┐ ┌──────────────────────────────┐
│ HTTP/WS 服务器 │ ──▶ │ Terminal / Lane / Cron │
│ 插件路由注册 │ │ Close Prelude + Handler 定义 │
│ 通道注册 │ │ Early Runtime 启动 │
│ watch-node 代理 │ │ Event Subscriptions 注册 │
└─────────────────┘ │ Runtime Services 启动 │
│ Gateway Handlers 组装 │
└──────────────────────────────┘
更多推荐



所有评论(0)