/
group.go
59 lines (50 loc) · 1.41 KB
/
group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package service
import (
"context"
"sync"
"github.com/hashicorp/go-multierror"
"golang.org/x/xerrors"
)
// Service describes a service for the Links 'R' Us monolithic application.
type Service interface {
// Name returns the service name.
Name() string
// Run executes the service and blocks until the context gets cancelled
// or an error occurs.
Run(context.Context) error
}
// Group is a list of Service instances that can execute in parallel.
type Group []Service
// Run executes all Service instances in the group using the provided context.
// Calls to Run block until all services have completed executing either because
// the context was cancelled or any of the services reported an error.
func (g Group) Run(ctx context.Context) error {
if ctx == nil {
ctx = context.Background()
}
runCtx, cancelFn := context.WithCancel(ctx)
defer cancelFn()
var wg sync.WaitGroup
errCh := make(chan error, len(g))
wg.Add(len(g))
for _, s := range g {
go func(s Service) {
defer wg.Done()
if err := s.Run(runCtx); err != nil {
errCh <- xerrors.Errorf("%s: %w", s.Name(), err)
cancelFn()
}
}(s)
}
// Keep running until the run context gets cancelled; then wait for
// all spawned service go-routines to exit
<-runCtx.Done()
wg.Wait()
// Collect and accumulate any reported errors.
var err error
close(errCh)
for srvErr := range errCh {
err = multierror.Append(err, srvErr)
}
return err
}