forked from saiya/dsps
/
probe.go
52 lines (45 loc) · 1.28 KB
/
probe.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
package multiplex
import (
"context"
"fmt"
"golang.org/x/sync/errgroup"
"github.com/m3dev/dsps/server/domain"
)
func (s *storageMultiplexer) Liveness(ctx context.Context) (interface{}, error) {
return s.probe(ctx, "liveness", func(ctx context.Context, s domain.Storage) (interface{}, error) {
return s.Liveness(ctx)
})
}
func (s *storageMultiplexer) Readiness(ctx context.Context) (interface{}, error) {
return s.probe(ctx, "readiness", func(ctx context.Context, s domain.Storage) (interface{}, error) {
return s.Readiness(ctx)
})
}
func (s *storageMultiplexer) probe(ctx context.Context, name string, delegate func(ctx context.Context, s domain.Storage) (interface{}, error)) (interface{}, error) {
g, ctx := errgroup.WithContext(ctx)
ch := make(chan childResult, len(s.children))
for id, child := range s.children {
id := id
child := child
g.Go(func() error {
result, err := delegate(ctx, child)
if err != nil {
return fmt.Errorf("Storage \"%s\" %s check failed: %w", id, name, err)
}
ch <- childResult{
id: id,
value: result,
}
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
close(ch)
return struct {
Children map[domain.StorageID]interface{} `json:"children"`
}{
Children: readAllChildResults(ch),
}, nil
}