Skip to content

Commit

Permalink
pr comments + 503 if not healthy
Browse files Browse the repository at this point in the history
  • Loading branch information
otherview committed Oct 29, 2024
1 parent 22bc2c2 commit df30451
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
8 changes: 7 additions & 1 deletion api/health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 2 additions & 1 deletion comm/communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
Expand Down
12 changes: 6 additions & 6 deletions health/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
}

0 comments on commit df30451

Please sign in to comment.