func (d dispatcher) Stop() error { var wait intsync.ErrorWaiter for _, i := range d.inbounds { wait.Submit(i.Stop) } for _, o := range d.outbounds { if o.Unary != nil { wait.Submit(o.Unary.Stop) } if o.Oneway != nil { wait.Submit(o.Oneway.Stop) } } if errors := wait.Wait(); len(errors) > 0 { return errorGroup(errors) } return nil }
func (d dispatcher) Start() error { var ( mu sync.Mutex startedInbounds []transport.Inbound startedOutbounds []transport.Outbound ) service := transport.ServiceDetail{ Name: d.Name, Registry: d, } startInbound := func(i transport.Inbound) func() error { return func() error { if err := i.Start(service, d.deps); err != nil { return err } mu.Lock() startedInbounds = append(startedInbounds, i) mu.Unlock() return nil } } startOutbound := func(o transport.Outbound) func() error { return func() error { if o == nil { return nil } if err := o.Start(d.deps); err != nil { return err } mu.Lock() startedOutbounds = append(startedOutbounds, o) mu.Unlock() return nil } } var wait intsync.ErrorWaiter for _, i := range d.inbounds { wait.Submit(startInbound(i)) } // TODO record the name of the service whose outbound failed for _, o := range d.outbounds { wait.Submit(startOutbound(o.Unary)) wait.Submit(startOutbound(o.Oneway)) } errors := wait.Wait() if len(errors) == 0 { return nil } // Failed to start so stop everything that was started. wait = intsync.ErrorWaiter{} for _, i := range startedInbounds { wait.Submit(i.Stop) } for _, o := range startedOutbounds { wait.Submit(o.Stop) } if newErrors := wait.Wait(); len(newErrors) > 0 { errors = append(errors, newErrors...) } return errorGroup(errors) }