Skip to content

Commit

Permalink
new endpoints /sync_status and /block_delay
Browse files Browse the repository at this point in the history
  • Loading branch information
e-asphyx committed Jul 19, 2024
1 parent a0516dd commit f2763fb
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
21 changes: 12 additions & 9 deletions health_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,40 @@ type HealthStatus struct {
BlockDelayOk bool `json:"block_delay_ok"`
}

func (h *HealthChecker) HealthStatus(ctx context.Context) (*HealthStatus, bool, error) {
var status HealthStatus
ok := true
func (s *HealthStatus) IsOk() bool {
return s.BlockDelayOk && s.IsBootstrapped && s.IsSynced
}

func (h *HealthChecker) HealthStatus(ctx context.Context) (*HealthStatus, error) {
status := HealthStatus{
IsBootstrapped: true,
IsSynced: true,
}
if h.CheckBootstrapped || h.CheckSyncState {
c, cancel := context.WithTimeout(ctx, h.Timeout)
defer cancel()
resp, err := h.Client.IsBootstrapped(c, h.ChainID)
if err != nil {
return nil, false, err
return nil, err
}
if h.CheckBootstrapped {
status.IsBootstrapped = resp.Bootstrapped
ok = ok && status.IsBootstrapped
}
if h.CheckSyncState {
status.IsSynced = resp.SyncState == client.SyncStateSynced
ok = ok && status.IsSynced
}
}
if h.CheckBlockDelay {
status.BlockDelayOk = h.Monitor.Status()
ok = ok && status.BlockDelayOk
}

if !ok {
if !status.IsOk() {
log.WithFields(log.Fields{
"chain_id": h.ChainID,
"bootstrapped": status.IsBootstrapped,
"synced": status.IsSynced,
"block_delay_ok": status.BlockDelayOk,
}).Warn("Chain health is not ok")
}
return &status, ok, nil
return &status, nil
}
38 changes: 36 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,48 @@ func main() {

r := mux.NewRouter()
r.Methods("GET").Path("/health").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
status, ok, err := checker.HealthStatus(r.Context())
status, err := checker.HealthStatus(r.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%v", err)
return
}
var code int
if ok {
if status.IsOk() {
code = http.StatusOK
} else {
code = http.StatusInternalServerError
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
json.NewEncoder(w).Encode(status)
})
r.Methods("GET").Path("/sync_status").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
status, err := checker.HealthStatus(r.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%v", err)
return
}
var code int
if status.IsBootstrapped && status.IsSynced {
code = http.StatusOK
} else {
code = http.StatusInternalServerError
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
json.NewEncoder(w).Encode(status)
})
r.Methods("GET").Path("/block_delay").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
status, err := checker.HealthStatus(r.Context())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "%v", err)
return
}
var code int
if status.BlockDelayOk {
code = http.StatusOK
} else {
code = http.StatusInternalServerError
Expand Down

0 comments on commit f2763fb

Please sign in to comment.