diff --git a/code/ch41/microkernel/agent.go b/code/ch41/microkernel/agent.go index da982d0..4424137 100644 --- a/code/ch41/microkernel/agent.go +++ b/code/ch41/microkernel/agent.go @@ -88,6 +88,7 @@ func (agt *Agent) startCollectors() error { var err error var errs CollectorsError var mutex sync.Mutex + for name, collector := range agt.collectors { go func(name string, collector Collector, ctx context.Context) { defer func() { @@ -101,6 +102,9 @@ func (agt *Agent) startCollectors() error { } }(name, collector, agt.ctx) } + if len(errs.CollectorErrors) == 0 { + return nil + } return errs } @@ -113,6 +117,10 @@ func (agt *Agent) stopCollectors() error { errors.New(name+":"+err.Error())) } } + if len(errs.CollectorErrors) == 0 { + return nil + } + return errs } @@ -125,6 +133,9 @@ func (agt *Agent) destoryCollectors() error { errors.New(name+":"+err.Error())) } } + if len(errs.CollectorErrors) == 0 { + return nil + } return errs } diff --git a/code/ch41/microkernel/agent_test.go b/code/ch41/microkernel/agent_test.go index 400cbad..4beae84 100644 --- a/code/ch41/microkernel/agent_test.go +++ b/code/ch41/microkernel/agent_test.go @@ -65,7 +65,9 @@ func TestAgent(t *testing.T) { c2 := NewCollect("c2", "2") agt.RegisterCollector("c1", c1) agt.RegisterCollector("c2", c2) - agt.Start() + if err := agt.Start(); err != nil { + fmt.Printf("start error %v\n", err) + } fmt.Println(agt.Start()) time.Sleep(time.Second * 1) agt.Stop()