/
healthcheck.go
50 lines (39 loc) · 1.29 KB
/
healthcheck.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
package app
import (
"context"
"fmt"
"net/http"
"time"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/gorilla/mux"
)
const maxNoHeightProgressDuration = 2 * time.Minute
func HealthcheckRegister(clientCtx client.Context, r *mux.Router) {
r.HandleFunc("/healthcheck", HealthcheckRequestHandlerFn(clientCtx)).Methods("GET")
}
func getLatestBlockTime(clientCtx client.Context) (time.Time, error) {
node, err := clientCtx.GetNode()
if err != nil {
return time.Time{}, err
}
status, err := node.Status(context.Background())
if err != nil {
return time.Time{}, err
}
return status.SyncInfo.LatestBlockTime, nil
}
func HealthcheckRequestHandlerFn(clientCtx client.Context) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
latestTime, err := getLatestBlockTime(clientCtx)
if err != nil {
rest.WriteErrorResponse(w, http.StatusInternalServerError, "Failed to get latest block time")
return
}
if time.Now().UTC().Sub(latestTime).Minutes() > maxNoHeightProgressDuration.Minutes() {
rest.WriteErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("Node is not syncing. Last block time: %s, curr time: %s", latestTime.String(), time.Now().UTC().String()))
return
}
w.WriteHeader(http.StatusOK)
}
}