From df30451451194df1a6f8814aa1579efbb5f4dace Mon Sep 17 00:00:00 2001 From: otherview Date: Tue, 29 Oct 2024 10:48:43 +0000 Subject: [PATCH] pr comments + 503 if not healthy --- api/health/health.go | 8 +++++++- comm/communicator.go | 3 ++- health/health.go | 12 ++++++------ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/api/health/health.go b/api/health/health.go index e0b047bbe..f7d0b0c73 100644 --- a/api/health/health.go +++ b/api/health/health.go @@ -23,11 +23,17 @@ func New(healthStatus *health.Health) *Health { } } -func (h *Health) handleGetHealth(w http.ResponseWriter, req *http.Request) error { +func (h *Health) handleGetHealth(w http.ResponseWriter, _ *http.Request) error { acc, err := h.healthStatus.Status() if err != nil { return err } + + if !acc.Healthy { + w.WriteHeader(http.StatusServiceUnavailable) // Set the status to 503 + } else { + w.WriteHeader(http.StatusOK) // Set the status to 200 + } return utils.WriteJSON(w, acc) } diff --git a/comm/communicator.go b/comm/communicator.go index 8a2656363..7fbef2e22 100644 --- a/comm/communicator.go +++ b/comm/communicator.go @@ -119,9 +119,10 @@ func (c *Communicator) Sync(ctx context.Context, handler HandleBlockStream) { syncCount++ if shouldSynced() { + c.health.ChainSyncStatus(false) delay = syncInterval c.onceSynced.Do(func() { - c.health.ChainSynced() + c.health.ChainSyncStatus(true) close(c.syncedCh) }) } diff --git a/health/health.go b/health/health.go index 56252ae01..7d39a94ce 100644 --- a/health/health.go +++ b/health/health.go @@ -13,8 +13,8 @@ import ( ) type BlockIngestion struct { - BestBlock *thor.Bytes32 `json:"bestBlock"` - BestBlockTimestamp *time.Time `json:"bestBlockTimestamp"` + BestBlock *thor.Bytes32 `json:"bestBlock"` + BestBlockIngestionTimestamp *time.Time `json:"bestBlockIngestionTimestamp"` } type Status struct { @@ -43,8 +43,8 @@ func (h *Health) Status() (*Status, error) { defer h.lock.RUnlock() blockIngest := &BlockIngestion{ - BestBlock: h.bestBlockID, - BestBlockTimestamp: &h.newBestBlock, + BestBlock: h.bestBlockID, + BestBlockIngestionTimestamp: &h.newBestBlock, } // todo review time slots @@ -58,9 +58,9 @@ func (h *Health) Status() (*Status, error) { }, nil } -func (h *Health) ChainSynced() { +func (h *Health) ChainSyncStatus(syncStatus bool) { h.lock.Lock() defer h.lock.Unlock() - h.chainSynced = true + h.chainSynced = syncStatus }