看一下做者本人的注释api
// Package bees is Beehive's central module system.
tcp
beehive 很是有趣的在于各逻辑的解耦设计,这不只让自己功能操做简单,也让扩展变得关注点少了不少,只须要一点学习成本就能够扩展本身的 beehivesvg
首先解释一下 bee hive 中 的概念函数
bee
表明的是咱们常见的 Worker
也就是说,实际的行为是由这些 小蜜蜂执行的。他们就相似于采蜜的工人,采集到了以后统一放回来处理工具
hive
是蜂房,也就是咱们常见的 WorkerPool
不一样的是,她更像一个 Facotry
,什么意思呢?她能够建立专属的 bee
。在极少的配置下,好比只须要配置上一个 token
便可。就能够生成一只 bee
专门针对某一种蜜工做了。oop
chain
又是什么? chain
就是连接事件与处理的工具,咱们认为 bee
采回蜜是一个事件,总不可能采回来啥都不干吧。针对不一样的 蜜 咱们就有不一样的反应,就有不一样的 action
好比某人的 blog 更新了 ,rss bee 接收到了以后飞回来,咱们就能够再要求 email bee 把这其中的信息经过邮件发给咱们或者咱们想发给的人。
这就须要 chain
来联系 event
和 action
了
学习
成组的 API 实现了 Resource 接口注册到 Container 的路由 Route 中。this
在文件中写好了 做者 实现的 Handle 的实现来注册 http 请求spa
API 只是供调用,逻辑重点在 bees 这个包里的实现。debug
首先是有的接口
// BeeInterface is an interface all bees implement. type BeeInterface interface { // Name of the bee Name() string // Namespace of the bee Namespace() string // Description of the bee Description() string // SetDescription sets a description SetDescription(s string) // Config returns this bees config Config() BeeConfig // Options of the bee Options() BeeOptions // SetOptions to configure the bee SetOptions(options BeeOptions) // ReloadOptions gets called after a bee's options get updated ReloadOptions(options BeeOptions) // Activates the bee Run(eventChannel chan Event) // Running returns the current state of the bee IsRunning() bool // Start the bee Start() // Stop the bee Stop() LastEvent() time.Time LogEvent() LastAction() time.Time LogAction() Logln(args ...interface{}) Logf(format string, args ...interface{}) LogErrorf(format string, args ...interface{}) LogFatal(args ...interface{}) SetSigChan(c chan bool) WaitGroup() *sync.WaitGroup // Handles an action Action(action Action) []Placeholder }
和他的基础实现
// Bee is the base-struct to be embedded by bee implementations. type Bee struct { config BeeConfig lastEvent time.Time lastAction time.Time Running bool SigChan chan bool waitGroup *sync.WaitGroup }
这里须要注意的是 Run 接口,在 Base-Struct Bee 中该方法
是空的实现,由于 Run 是 Bee 的生命周期开始处,是自动开始的。
简单的看某一个实现便可
// WebBee is a Bee that starts an HTTP server and fires events for incoming // requests. type WebBee struct { bees.Bee addr string eventChan chan bees.Event }
能够很清楚的指导,这个 WebBee 中的 eventChan 正是通知的地方,也就是上文所说的 Chain 的开始处。注意的是因为松耦合的设计,任何 Bee 均可以成为 Chain 上的一环,只要它能触发事件。或者监听事件。
// Run executes the Bee's event loop. func (mod *WebBee) Run(cin chan bees.Event) { mod.eventChan = cin srv := &http.Server{Addr: mod.addr, Handler: mod} l, err := net.Listen("tcp", mod.addr) if err != nil { mod.LogErrorf("Can't listen on %s", mod.addr) return } defer l.Close() go func() { err := srv.Serve(l) if err != nil { mod.LogErrorf("Server error: %v", err) } // Go 1.8+: srv.Close() }() select { case <-mod.SigChan: return } }
同时 WebBee 也有一个方法 ServeHTTP 来实现 http.Handle 来处理请求。
这里也就是前文所说的 注册的那些 API 的部分来源,每个 bee 自身实现的自动注册暴露给外界调用。
func (mod *WebBee) ServeHTTP(w http.ResponseWriter, req *http.Request)
package:beehive/bees/event.go
刚才讲到了 触发事件 event 的 WebBee 实现,如今咱们来看 event 的实现
其实是经过 这个函数实现的
// handleEvents handles incoming events and executes matching Chains. func handleEvents() { for { event, ok := <-eventsIn ··· bee := GetBee(event.Bee) (*bee).LogEvent() ··· go func() { defer func() { if e := recover(); e != nil { log.Printf("Fatal chain event: %s %s", e, debug.Stack()) } }() execChains(&event) }() } }
省略了 日志部分。能够看到 handleEvents 经过接受通道里的 event
,并检查 event
中的 Bee
做为 标志找到对应的 Bee 唤醒。
这里咱们能够看到 最后进入了 Chains 中执行,即上文所说的 Chain 将 Event 和 Action 连接了起来,让 Bee 之间可以协做。
package:beehive/bees/chains.go
chain 中其实是调用 Actions 经过下面的 execActions 函数
for _, el := range c.Actions { action := GetAction(el) if action == nil { log.Println("\t\tERROR: Unknown action referenced!") continue } execAction(*action, m) }
咱们来看看 Action 的执行。
package: beehive/bees/actions.go
actions 既能够运行设置中的 options 也能够直接在 运行函数中传入须要运行的 optionsfunc execAction(action Action, opts map[string]interface{}) bool
整个执行逻辑是如此了,其余还有一些