Skip to content

Commit 614371c

Browse files
committed
Refactor the time helper to a shared function. Make the DNS healthcheck also report down based on the same time threshold
1 parent 3e1f09e commit 614371c

File tree

3 files changed

+59
-60
lines changed

3 files changed

+59
-60
lines changed

internal/healthcheck/dns.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package healthcheck
22

33
import (
44
"context"
5-
"fmt"
65
"net"
76
"net/http"
87
"time"
@@ -34,18 +33,16 @@ func (h *Healthcheck) DNSCheckLoop() {
3433
ips, err := r.LookupIP(context.TODO(), "ip", hostname)
3534
if err != nil {
3635
log.Printf("Fetching dns records failed: %s\n", err.Error())
37-
h.dnsOK = false
3836
return
3937
}
4038

4139
if len(ips) > 0 {
4240
log.Println("Received at least 1 IP. Ready!")
43-
h.dnsOK = true
41+
h.lastDNSTime = time.Now()
4442
return
4543
}
4644

4745
log.Println("Received NO IPs. Not Ready!")
48-
h.dnsOK = false
4946
}()
5047

5148
time.Sleep(30 * time.Second)
@@ -55,18 +52,6 @@ func (h *Healthcheck) DNSCheckLoop() {
5552
// seederHealthcheck endpoint for the seeder service as a whole (Are we sending DNS responses)
5653
func (h *Healthcheck) seederHealthcheck() func(http.ResponseWriter, *http.Request) {
5754
return func(w http.ResponseWriter, r *http.Request) {
58-
if h.dnsOK {
59-
w.WriteHeader(http.StatusOK)
60-
_, err := fmt.Fprintf(w, "Ok")
61-
if err != nil {
62-
log.Errorf("Error writing healthcheck response %s\n", err.Error())
63-
}
64-
} else {
65-
w.WriteHeader(http.StatusInternalServerError)
66-
_, err := fmt.Fprintf(w, "Not OK")
67-
if err != nil {
68-
log.Errorf("Error writing healthcheck response %s\n", err.Error())
69-
}
70-
}
55+
timeMetricHealthcheckHelper(h.lastDNSTime, w, r)
7156
}
7257
}

internal/healthcheck/fullnode.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package healthcheck
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"time"
7+
8+
log "github.com/sirupsen/logrus"
9+
10+
"github.com/chia-network/go-chia-libs/pkg/types"
11+
)
12+
13+
func (h *Healthcheck) fullNodeReceive(resp *types.WebsocketResponse) {
14+
var blockHeight uint32
15+
16+
if resp.Command != "get_blockchain_state" {
17+
return
18+
}
19+
20+
block := &types.WebsocketBlockchainState{}
21+
err := json.Unmarshal(resp.Data, block)
22+
if err != nil {
23+
log.Errorf("Error unmarshalling: %s\n", err.Error())
24+
return
25+
}
26+
blockHeight = block.BlockchainState.Peak.OrEmpty().Height
27+
28+
// Edge case, but we should be sure block height is increasing
29+
if blockHeight <= h.lastHeight {
30+
return
31+
}
32+
33+
h.lastHeight = blockHeight
34+
h.lastHeightTime = time.Now()
35+
}
36+
37+
// Healthcheck endpoint for the full node service as a whole
38+
func (h *Healthcheck) fullNodeHealthcheck() func(http.ResponseWriter, *http.Request) {
39+
return func(w http.ResponseWriter, r *http.Request) {
40+
timeMetricHealthcheckHelper(h.lastHeightTime, w, r)
41+
}
42+
}

internal/healthcheck/healthcheck.go

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package healthcheck
22

33
import (
4-
"encoding/json"
54
"fmt"
65
"net/http"
76
"net/url"
@@ -25,8 +24,8 @@ type Healthcheck struct {
2524
// Time we received the last block height
2625
lastHeightTime time.Time
2726

28-
// dnsOK Are we currently serving DNS responses?
29-
dnsOK bool
27+
// Last time we got a successful DNS response
28+
lastDNSTime time.Time
3029
}
3130

3231
// NewHealthcheck returns a new instance of healthcheck
@@ -35,7 +34,6 @@ func NewHealthcheck(port uint16, logLevel log.Level) (*Healthcheck, error) {
3534

3635
healthcheck := &Healthcheck{
3736
healthcheckPort: port,
38-
dnsOK: false,
3937
}
4038

4139
log.SetLevel(logLevel)
@@ -103,30 +101,6 @@ func (h *Healthcheck) websocketReceive(resp *types.WebsocketResponse, err error)
103101
}
104102
}
105103

106-
func (h *Healthcheck) fullNodeReceive(resp *types.WebsocketResponse) {
107-
var blockHeight uint32
108-
109-
if resp.Command != "get_blockchain_state" {
110-
return
111-
}
112-
113-
block := &types.WebsocketBlockchainState{}
114-
err := json.Unmarshal(resp.Data, block)
115-
if err != nil {
116-
log.Errorf("Error unmarshalling: %s\n", err.Error())
117-
return
118-
}
119-
blockHeight = block.BlockchainState.Peak.OrEmpty().Height
120-
121-
// Edge case, but we should be sure block height is increasing
122-
if blockHeight <= h.lastHeight {
123-
return
124-
}
125-
126-
h.lastHeight = blockHeight
127-
h.lastHeightTime = time.Now()
128-
}
129-
130104
func (h *Healthcheck) walletReceive(resp *types.WebsocketResponse) {}
131105

132106
func (h *Healthcheck) crawlerReceive(resp *types.WebsocketResponse) {}
@@ -150,21 +124,19 @@ func (h *Healthcheck) reconnectHandler() {
150124
}
151125
}
152126

153-
// Healthcheck endpoint for the full node service as a whole
154-
func (h *Healthcheck) fullNodeHealthcheck() func(http.ResponseWriter, *http.Request) {
155-
return func(w http.ResponseWriter, r *http.Request) {
156-
if time.Since(h.lastHeightTime) < viper.GetDuration("healthcheck-threshold") {
157-
w.WriteHeader(http.StatusOK)
158-
_, err := fmt.Fprintf(w, "Ok")
159-
if err != nil {
160-
log.Errorf("Error writing healthcheck response %s\n", err.Error())
161-
}
162-
} else {
163-
w.WriteHeader(http.StatusInternalServerError)
164-
_, err := fmt.Fprintf(w, "Not OK")
165-
if err != nil {
166-
log.Errorf("Error writing healthcheck response %s\n", err.Error())
167-
}
127+
func timeMetricHealthcheckHelper(lastTime time.Time, w http.ResponseWriter, r *http.Request) {
128+
if time.Since(lastTime) < viper.GetDuration("healthcheck-threshold") {
129+
w.WriteHeader(http.StatusOK)
130+
_, err := fmt.Fprintf(w, "Ok")
131+
if err != nil {
132+
log.Errorf("Error writing healthcheck response %s\n", err.Error())
133+
}
134+
} else {
135+
w.WriteHeader(http.StatusInternalServerError)
136+
_, err := fmt.Fprintf(w, "Not OK")
137+
if err != nil {
138+
log.Errorf("Error writing healthcheck response %s\n", err.Error())
168139
}
169140
}
170141
}
142+

0 commit comments

Comments
 (0)