-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
multi.go
84 lines (74 loc) · 1.91 KB
/
multi.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package services
import (
"context"
"io"
"sync"
"go.uber.org/multierr"
)
// StartClose is a subset of the ServiceCtx interface.
type StartClose interface {
Start(context.Context) error
Close() error
}
// MultiStart is a utility for starting multiple services together.
// The set of started services is tracked internally, so that they can be closed if any single service fails to start.
type MultiStart struct {
started []StartClose
}
// Start attempts to Start all services. If any service fails to start, the previously started services will be
// Closed, and an error returned.
func (m *MultiStart) Start(ctx context.Context, srvcs ...StartClose) (err error) {
for _, s := range srvcs {
err = m.start(ctx, s)
if err != nil {
return err
}
}
return
}
func (m *MultiStart) start(ctx context.Context, s StartClose) (err error) {
err = s.Start(ctx)
if err != nil {
err = multierr.Append(err, m.Close())
} else {
m.started = append(m.started, s)
}
return
}
// Close closes all started services, in reverse order.
func (m *MultiStart) Close() (err error) {
for i := len(m.started) - 1; i >= 0; i-- {
s := m.started[i]
err = multierr.Append(err, s.Close())
}
return
}
// CloseBecause calls Close and returns reason along with any additional errors.
func (m *MultiStart) CloseBecause(reason error) (err error) {
return multierr.Append(reason, m.Close())
}
// MultiClose is a utility for closing multiple services concurrently.
type MultiClose []io.Closer
// Close closes alls service concurrently and collects any returned errors as a multierr.
func (m MultiClose) Close() (err error) {
if len(m) == 0 {
return nil
}
var wg sync.WaitGroup
wg.Add(len(m))
errs := make(chan error, len(m))
for _, s := range m {
go func(c io.Closer) {
defer wg.Done()
if e := c.Close(); e != nil {
errs <- e
}
}(s)
}
wg.Wait()
close(errs)
for e := range errs {
err = multierr.Append(err, e)
}
return
}