From f2763fb3a0dd3de5e0433c8a6a46c472692994fc Mon Sep 17 00:00:00 2001 From: Eugene Zagidullin Date: Fri, 19 Jul 2024 21:53:01 +0300 Subject: [PATCH] new endpoints /sync_status and /block_delay --- health_checker.go | 21 ++++++++++++--------- main.go | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 11 deletions(-) diff --git a/health_checker.go b/health_checker.go index c3011fe..fcf4ccf 100644 --- a/health_checker.go +++ b/health_checker.go @@ -26,31 +26,34 @@ 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, @@ -58,5 +61,5 @@ func (h *HealthChecker) HealthStatus(ctx context.Context) (*HealthStatus, bool, "block_delay_ok": status.BlockDelayOk, }).Warn("Chain health is not ok") } - return &status, ok, nil + return &status, nil } diff --git a/main.go b/main.go index 3bb9ca2..fddf567 100644 --- a/main.go +++ b/main.go @@ -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