-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
must.go
50 lines (43 loc) · 1.16 KB
/
must.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
package services
import (
"context"
"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
}