「连载」边缘计算(二十四)03-04:边缘部分源码(源码分析篇)

(接上篇)

在Register()函数中对EdgeHub struct的初始化只是对EdgeHub struct中的controller进行初始化。controller的初始化函数具体如下所示。

|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| KubeEdge/edge/pkg/edgehub/controller.go |
| //NewEdgeHubController creates and returns a EdgeHubController object func NewEdgeHubController() *Controller { return &Controller{ config: &config.GetConfig().CtrConfig, stopChan: make(chan struct{}), syncKeeper: make(mapstringchan model.Message), } } |

NewEdgeHubController()函数中嵌套了一个获取配置信息的函数调用,具体如下所示。

|----------------------------------------|
| config: &config.GetConfig().CtrConfig |

config.GetConfig()函数定义具体如下所示。

|------------------------------------------------------------------------------------------------------------------------------------------------------|
| KubeEdge/edge/pkg/edgehub/config/config.go |
| var edgeHubConfig EdgeHubConfig ... //GetConfig returns the EdgeHub configuration object func GetConfig() *EdgeHubConfig { return &edgeHubConfig } |

GetConfig()函数只返回了&edgeHubConfig,而edgeHubConfig是一个EdgeHubConfig struct类型的全局变量。至于该变量是在哪里被赋值,怎么赋值的,暂且留个疑问------* EdgeHubConfig赋值疑问。

到此,EdgeHub struct的初始化就告一段落了。下面分析EdgeHub的启动函数,具体如下所示。

|-----------------------------------------------------------------------------------------------------------------------------------------|
| KubeEdge/edge/pkg/edgehub/module.go |
| //Start sets context and starts the controller func (eh *EdgeHub) Start(c *context.Context) { eh.context = c eh.controller.Start(c) } |

EdgeHub启动函数Start()只做了两件事。

  1. 接收并存储传入的消息管道;
  2. 启动EdgeHub的controller。

由前面的分析可知,EdgeHub的controller作为EdgeHub功能的主要载体。其启动函数囊括EdgeHub绝大部分启动逻辑。继续进入controller的启动函数,具体如下所示。

|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| KubeEdge/edge/pkg/edgehub/controller.go |
| //Start will start EdgeHub func (ehc *Controller) Start(ctx *context.Context) { config.InitEdgehubConfig() for { err := ehc.initial(ctx) ... err = ehc.chClient.Init() ... // execute hook func after connect ehc.pubConnectInfo(true) go ehc.routeToEdge() go ehc.routeToCloud() go ehc.keepalive() // wait the stop singal // stop authinfo manager/websocket connection <-ehc.stopChan ehc.chClient.Uninit() // execute hook fun after disconnect ehc.pubConnectInfo(false) // sleep one period of heartbeat, then try to connect cloud hub again time.Sleep(ehc.config.HeartbeatPeriod * 2) // clean channel clean: for { select { case <-ehc.stopChan: default: break clean } } } } |

从Controller的启动函数Start()的定义,可以清楚地看到其包含了EdgehubConfig初始化、各种业务go routine的启动和最后的退出清理。下面逐个深入剖析。

「 未完待续...... 」

相关推荐
怕浪猫3 小时前
这个十几个agent 协作项目值得关注
人工智能·面试·github
starzy19904 小时前
深入理解Scala隐式类:扩展方法机制
开发语言·人工智能·scala
深蓝AI5 小时前
为什么 agent 的 demo 都很惊艳,上了生产就不靠谱
人工智能·程序员
数字新视界5 小时前
信创动环监控在机房环境管理中的智能化应用与实践分析
大数据·人工智能·物联网·信息与通信·动环监控系统
岁月宁静5 小时前
DeepSeek Harness 实战与架构解析:让 Agent 从“会聊”到“能交付”
人工智能·python·agent
深蓝AI6 小时前
一个项目五个 AI 同时干活,Claude Code 怎么让它们不互相打架
人工智能·程序员
forest_096 小时前
多 Agent 协作第二课:交叉验证≠独立验证
人工智能·python·算法·机器学习·prompt·agi
ZONGYUAN-ROOT7 小时前
【工具治理】Nginx换机丢16子域事故复盘
人工智能
dozenyaoyida7 小时前
AI与大模型新闻日报 | 2026-11-08
人工智能·搜索引擎·ai·大模型·新闻
泯泷7 小时前
AI Agent 工具设计:读完一百页 PDF,不等于真的看懂了
人工智能·llm·agent