公司动态
openclaw源码解读(6)——server.impl.ts 真正的启动引擎 第三阶段:网络栈启动(HTTP + WS)2
第四阶段运行时激活Bring It AliveL1514 - 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-AttachL2109-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中”关闭生命周期”块2L1364-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 启动gatewayL1495-2389块3-1L1512-1557 — 早期运行时启动Early Runtimetry {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-2L1559-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-3L1593-2206 — Gateway 处理器Handlers这是核心部分// 1. 操作员审批系统 L1601-1610const approvalSessionEvents createOperatorApprovalSessionEventRuntime({...});// 2. 创建三大审批管理器处理 exec 审批、插件审批、system-agent 审批的 WebSocket 事件推送 L1612-1639const {execApprovalManager, // exec 命令审批pluginApprovalManager, // 插件审批systemAgentApprovalManager, // 系统 agent 审批extraHandlers, // 额外的网关方法处理器辅助方法coreGatewayHandlers, // 核心网关方法来自 server-methods.js} ...// 3. 注册审批重放 L1640-1642approvalManagersForReplay.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-1675coreDescriptorHandlers - filter过滤 - 构建核心方法描述器coreDescriptors L1676-1683coreDescriptors及其他描述器 - 注册gateway方法并返回// ② 注册插件方法pluginRegistry.gatewayHandlersL1705-1726总结当下的阶段性结构第三阶段网络栈 第四阶段运行时组装┌─────────────────┐ ┌──────────────────────────────┐│ HTTP/WS 服务器 │ ──▶ │ Terminal / Lane / Cron ││ 插件路由注册 │ │ Close Prelude Handler 定义 ││ 通道注册 │ │ Early Runtime 启动 ││ watch-node 代理 │ │ Event Subscriptions 注册 │└─────────────────┘ │ Runtime Services 启动 ││ Gateway Handlers 组装 │└──────────────────────────────┘