make atomic

This commit is contained in:
Cory Schwartz 2022-05-23 12:04:13 -07:00
parent 0a7fbd0fce
commit 7d55ab0734

View File

@ -3,6 +3,7 @@ package node
import ( import (
"context" "context"
"net/http" "net/http"
"sync/atomic"
"time" "time"
lapi "github.com/filecoin-project/lotus/api" lapi "github.com/filecoin-project/lotus/api"
@ -17,11 +18,11 @@ type HealthHandler struct {
} }
func (h *HealthHandler) SetHealthy(healthy bool) { func (h *HealthHandler) SetHealthy(healthy bool) {
h := int32(0) var hi32 int32
if healthy { if healthy {
h = 1 hi32 = 1
} }
atomic.StoreInt32(&h.healthy, h) atomic.StoreInt32(&h.healthy, hi32)
} }
func (h *HealthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (h *HealthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
@ -32,30 +33,30 @@ func (h *HealthHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
// The backend is considered alive so long as there have been recent // Check that the node is still working. That is, that it's still processing the chain.
// head changes. Being alive doesn't mean we are up to date, just moving. // If there have been no recent changes, consider the node to be dead.
func NewLiveHandler(api lapi.FullNode) *HealthHandler { func NewLiveHandler(api lapi.FullNode) *HealthHandler {
ctx := context.Background() ctx := context.Background()
h := HealthHandler{} h := HealthHandler{}
go func() { go func() {
const reset = 5 const reset int32 = 5
var countdown = 0 var countdown int32 = 0
minutely := time.NewTicker(time.Minute) minutely := time.NewTicker(time.Minute)
headCh, err := api.ChainNotify(ctx) headCh, err := api.ChainNotify(ctx)
if err != nil { if err != nil {
healthlog.Warnf("failed to instantiate chain notify channel; liveliness cannot be determined. %s", err) healthlog.Warnf("failed to instantiate chain notify channel; liveness cannot be determined. %s", err)
h.SetHealthy(false) h.SetHealthy(false)
return return
} }
for { for {
select { select {
case <-minutely.C: case <-minutely.C:
countdown = countdown - 1 atomic.AddInt32(&countdown, -1)
if countdown == 0 { if countdown <= 0 {
h.SetHealthy(false) h.SetHealthy(false)
} }
case <-headCh: case <-headCh:
countdown = reset atomic.StoreInt32(&countdown, reset)
h.SetHealthy(true) h.SetHealthy(true)
} }
} }
@ -64,8 +65,8 @@ func NewLiveHandler(api lapi.FullNode) *HealthHandler {
} }
// Check if we are ready to handle traffic. // Check if we are ready to handle traffic.
// 1. sync workers are caught up. // 1. sync workers are reasonably up to date.
// 2 // 2. libp2p is servicable
func NewReadyHandler(api lapi.FullNode) *HealthHandler { func NewReadyHandler(api lapi.FullNode) *HealthHandler {
ctx := context.Background() ctx := context.Background()
h := HealthHandler{} h := HealthHandler{}