External
Group
Group 用于并发的执行一组 actor,每个 actor 有两个方法,如下:
type actor struct {execute func() errorinterrupt func(error)}
执行过程如下所示,需要注意的是,因为每个 actor 必然返回 error 或 nil,因此,接受错误的 chan 会在第一个执行完成的 actor 返回后触发。
func (g *Group) Run() error {if len(g.actors) == 0 {return nil}// Run each actor.errors := make(chan error, len(g.actors))for _, a := range g.actors {go func(a actor) {errors <- a.execute()}(a)}// Wait for the first actor to stop.err := <-errors// Signal all actors to stop.for _, a := range g.actors {a.interrupt(err)}// Wait for all actors to stop.for i := 1; i < cap(errors); i++ {<-errors}// Return the original error.return err}
分析:根据 Group 的设计,一个任务执行出错或执行完成后,其他任务都要停止运行,方便管理系统中的核心任
