-
Notifications
You must be signed in to change notification settings - Fork 168
/
health.go
77 lines (63 loc) · 1.77 KB
/
health.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
package controller
import (
"context"
gosundheit "github.com/AppsFlyer/go-sundheit"
"github.com/AppsFlyer/go-sundheit/checks"
"github.com/openziti/metrics"
"github.com/openziti/storage/boltz"
"github.com/pkg/errors"
"go.etcd.io/bbolt"
"sync/atomic"
"time"
)
func (c *Controller) initializeHealthChecks() (gosundheit.Health, error) {
healthChecker := gosundheit.New()
check, err := checks.NewPingCheck("bolt.read", &boltPinger{
dbProvider: c.network.GetDb,
openReadTxs: c.GetNetwork().GetMetricsRegistry().Gauge("bolt.open_read_txs"),
})
if err != nil {
return nil, err
}
err = healthChecker.RegisterCheck(check,
gosundheit.InitialDelay(c.config.HealthChecks.BoltCheck.InitialDelay),
gosundheit.ExecutionPeriod(c.config.HealthChecks.BoltCheck.Interval),
gosundheit.ExecutionTimeout(c.config.HealthChecks.BoltCheck.Timeout),
gosundheit.InitiallyPassing(true))
if err != nil {
return nil, err
}
return healthChecker, nil
}
type boltPinger struct {
dbProvider func() boltz.Db
openReadTxs metrics.Gauge
running atomic.Bool
}
func (self *boltPinger) PingContext(ctx context.Context) error {
if !self.running.CompareAndSwap(false, true) {
return errors.Errorf("previous bolt ping is still running")
}
deadline, hasDeadline := ctx.Deadline()
checkFunc := func(tx *bbolt.Tx) error {
self.openReadTxs.Update(int64(tx.DB().Stats().OpenTxN))
return nil
}
if !hasDeadline {
defer self.running.Store(false)
return self.dbProvider().View(checkFunc)
}
errC := make(chan error, 1)
go func() {
defer self.running.Store(false)
errC <- self.dbProvider().View(checkFunc)
}()
timer := time.NewTimer(time.Until(deadline))
defer timer.Stop()
select {
case err := <-errC:
return err
case <-timer.C:
return errors.Errorf("bolt ping timed out")
}
}