(接上篇)
context.GetContext()函数定义具体如下所示。
| KubeEdge/beehive/pkg/core/context/contex_factory.go |
|---|
| // GetContext gets global context instancefunc GetContext(contextType string) *Context {once.Do(func() {context = &Context{}switch contextType {case MsgCtxTypeChannel:channelContext := NewChannelContext()context.messageContext = channelContextcontext.moduleContext = channelContextdefault:klog.Warningf("Do not support context type:%s", contextType)}})return context} |
context.GetContext()函数定义中的第3行context = &Context{}实例化了一个空context。下面我们分析该实例化context。context 结构体具体如下所示。
| KubeEdge/beehive/pkg/core/context/context.go |
|---|
| // Context is global context objecttype Context struct {moduleContext ModuleContextmessageContext MessageContext} //ModuleContext is interface for context module managementtype ModuleContext interface {AddModule(module string)AddModuleGroup(module, group string)Cleanup(module string)}//MessageContext is interface for message syncingtype MessageContext interface {// async modeSend(module string, message model.Message)Receive(module string) (model.Message, error)// sync modeSendSync(module string, message model.Message, timeout time.Duration) ( model.Message, error)SendResp(message model.Message)// group broadcastSendToGroup(moduleType string, message model.Message)SendToGroupSync(moduleType string, message model.Message, timeout time.Duration) error} |
从上面的Context结构体,可以看出该Context的两个属性——moduleContext和messageContext,它们都是interface类型,所以可以断定该Context不是真身。在函数GetContext()(KubeEdge/beehive/pkg/core/context/context.go)继续往下看,在第6行channelContext := NewChannelContext()有一个context实例化函数NewChannelContext(),进入该函数的定义去看一下它是不是真身。
ChannelContext struct定义如下所示。
| KubeEdge/beehive/pkg/core/context/context.go |
|---|
| // ChannelContext is object for Context channeltype ChannelContext struct {//ConfigFactory goarchaius.ConfigurationFactorychannels map[string]chan model.MessagechsLock sync.RWMutextypeChannels map[string]map[string]chan model.MessagetypeChsLock sync.RWMutexanonChannels map[string]chan model.MessageanonChsLock sync.RWMutex} |
上述代码中,ChannelContext 函数实现了Context struct(KubeEdge/beehive/pkg/core/context/context.go)属性包含的所有interface(ModuleContext,MessageContext)。毫无疑问,ChannelContext struct就是CloudCore中各功能模块相互通信的消息队列框架的真身了。
至于ChannelContext struct在CloudCore中各功能模块是如何相互通信的,感兴趣的读者可以根据本文的梳理自己去深入剖析。
「未完待续……」 点击下方标题可阅读技术文章