Skip to content

Commit

Permalink
Merge pull request #11 from Chia-Network/dns-intro-time-threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
cmmarslender authored Oct 3, 2023
2 parents 3e1f09e + 614371c commit 54d04c1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 60 deletions.
19 changes: 2 additions & 17 deletions internal/healthcheck/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package healthcheck

import (
"context"
"fmt"
"net"
"net/http"
"time"
Expand Down Expand Up @@ -34,18 +33,16 @@ func (h *Healthcheck) DNSCheckLoop() {
ips, err := r.LookupIP(context.TODO(), "ip", hostname)
if err != nil {
log.Printf("Fetching dns records failed: %s\n", err.Error())
h.dnsOK = false
return
}

if len(ips) > 0 {
log.Println("Received at least 1 IP. Ready!")
h.dnsOK = true
h.lastDNSTime = time.Now()
return
}

log.Println("Received NO IPs. Not Ready!")
h.dnsOK = false
}()

time.Sleep(30 * time.Second)
Expand All @@ -55,18 +52,6 @@ func (h *Healthcheck) DNSCheckLoop() {
// seederHealthcheck endpoint for the seeder service as a whole (Are we sending DNS responses)
func (h *Healthcheck) seederHealthcheck() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if h.dnsOK {
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprintf(w, "Ok")
if err != nil {
log.Errorf("Error writing healthcheck response %s\n", err.Error())
}
} else {
w.WriteHeader(http.StatusInternalServerError)
_, err := fmt.Fprintf(w, "Not OK")
if err != nil {
log.Errorf("Error writing healthcheck response %s\n", err.Error())
}
}
timeMetricHealthcheckHelper(h.lastDNSTime, w, r)
}
}
42 changes: 42 additions & 0 deletions internal/healthcheck/fullnode.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package healthcheck

import (
"encoding/json"
"net/http"
"time"

log "github.com/sirupsen/logrus"

"github.com/chia-network/go-chia-libs/pkg/types"
)

func (h *Healthcheck) fullNodeReceive(resp *types.WebsocketResponse) {
var blockHeight uint32

if resp.Command != "get_blockchain_state" {
return
}

block := &types.WebsocketBlockchainState{}
err := json.Unmarshal(resp.Data, block)
if err != nil {
log.Errorf("Error unmarshalling: %s\n", err.Error())
return
}
blockHeight = block.BlockchainState.Peak.OrEmpty().Height

// Edge case, but we should be sure block height is increasing
if blockHeight <= h.lastHeight {
return
}

h.lastHeight = blockHeight
h.lastHeightTime = time.Now()
}

// Healthcheck endpoint for the full node service as a whole
func (h *Healthcheck) fullNodeHealthcheck() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
timeMetricHealthcheckHelper(h.lastHeightTime, w, r)
}
}
58 changes: 15 additions & 43 deletions internal/healthcheck/healthcheck.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package healthcheck

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
Expand All @@ -25,8 +24,8 @@ type Healthcheck struct {
// Time we received the last block height
lastHeightTime time.Time

// dnsOK Are we currently serving DNS responses?
dnsOK bool
// Last time we got a successful DNS response
lastDNSTime time.Time
}

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

healthcheck := &Healthcheck{
healthcheckPort: port,
dnsOK: false,
}

log.SetLevel(logLevel)
Expand Down Expand Up @@ -103,30 +101,6 @@ func (h *Healthcheck) websocketReceive(resp *types.WebsocketResponse, err error)
}
}

func (h *Healthcheck) fullNodeReceive(resp *types.WebsocketResponse) {
var blockHeight uint32

if resp.Command != "get_blockchain_state" {
return
}

block := &types.WebsocketBlockchainState{}
err := json.Unmarshal(resp.Data, block)
if err != nil {
log.Errorf("Error unmarshalling: %s\n", err.Error())
return
}
blockHeight = block.BlockchainState.Peak.OrEmpty().Height

// Edge case, but we should be sure block height is increasing
if blockHeight <= h.lastHeight {
return
}

h.lastHeight = blockHeight
h.lastHeightTime = time.Now()
}

func (h *Healthcheck) walletReceive(resp *types.WebsocketResponse) {}

func (h *Healthcheck) crawlerReceive(resp *types.WebsocketResponse) {}
Expand All @@ -150,21 +124,19 @@ func (h *Healthcheck) reconnectHandler() {
}
}

// Healthcheck endpoint for the full node service as a whole
func (h *Healthcheck) fullNodeHealthcheck() func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
if time.Since(h.lastHeightTime) < viper.GetDuration("healthcheck-threshold") {
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprintf(w, "Ok")
if err != nil {
log.Errorf("Error writing healthcheck response %s\n", err.Error())
}
} else {
w.WriteHeader(http.StatusInternalServerError)
_, err := fmt.Fprintf(w, "Not OK")
if err != nil {
log.Errorf("Error writing healthcheck response %s\n", err.Error())
}
func timeMetricHealthcheckHelper(lastTime time.Time, w http.ResponseWriter, r *http.Request) {
if time.Since(lastTime) < viper.GetDuration("healthcheck-threshold") {
w.WriteHeader(http.StatusOK)
_, err := fmt.Fprintf(w, "Ok")
if err != nil {
log.Errorf("Error writing healthcheck response %s\n", err.Error())
}
} else {
w.WriteHeader(http.StatusInternalServerError)
_, err := fmt.Fprintf(w, "Not OK")
if err != nil {
log.Errorf("Error writing healthcheck response %s\n", err.Error())
}
}
}

0 comments on commit 54d04c1

Please sign in to comment.