-
Notifications
You must be signed in to change notification settings - Fork 3
/
health.go
83 lines (73 loc) · 1.75 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
78
79
80
81
82
83
package main
import (
"context"
"encoding/json"
"net/http"
"time"
tezos "github.com/ecadlabs/tezos_exporter/go-tezos"
log "github.com/sirupsen/logrus"
)
type HealthHandler struct {
service *tezos.Service
interval time.Duration
chainID string
threshold int
tcount int
ok bool
}
func (h *HealthHandler) poll() {
status, err := h.service.GetBootstrapped(context.Background(), h.chainID)
if err != nil {
log.WithError(err).Error("error getting bootstrap status")
h.ok = false
} else {
h.ok = status.Bootstrapped && status.SyncState == tezos.SyncStateSynced
}
h.tcount = h.threshold
tick := time.Tick(h.interval)
for range tick {
status, err := h.service.GetBootstrapped(context.Background(), h.chainID)
if err != nil {
log.WithError(err).Error("error getting bootstrap status")
h.ok = false
h.tcount = h.threshold
continue
}
ok := status.Bootstrapped && status.SyncState == tezos.SyncStateSynced
if ok != h.ok {
h.tcount--
if h.tcount == 0 {
h.tcount = h.threshold
h.ok = ok
}
} else {
h.tcount = h.threshold
}
}
}
func (h *HealthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var res struct {
Bootstrapped bool `json:"bootstrapped"`
}
var status int
if h.ok {
status = http.StatusOK
res.Bootstrapped = true
} else {
status = http.StatusInternalServerError
res.Bootstrapped = false
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(status)
json.NewEncoder(w).Encode(&res)
}
func NewHealthHandler(service *tezos.Service, chainID string, interval time.Duration, threshold int) *HealthHandler {
h := HealthHandler{
service: service,
interval: interval,
threshold: threshold,
chainID: chainID,
}
go h.poll()
return &h
}