Skip to content

Commit

Permalink
fix: ensure watcher readiness is read atomically (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKetmo authored Sep 15, 2023
1 parent bafeeeb commit 2c1e984
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions pkg/watcher/watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package watcher
import (
"context"
"fmt"
"sync/atomic"
"time"

"github.com/avast/retry-go/v4"
Expand All @@ -27,8 +28,8 @@ type Config struct {
type Watcher struct {
cfg *Config

isSynced atomic.Bool
rpcClient *http.HTTP
status *ctypes.ResultStatus
validators []*types.Validator
}

Expand All @@ -46,9 +47,7 @@ func New(config *Config) (*Watcher, error) {
}

func (w *Watcher) Ready() bool {
return w.status != nil &&
w.status.SyncInfo.CatchingUp == false &&
len(w.validators) > 0
return w.isSynced.Load()
}

func (w *Watcher) Start(ctx context.Context) error {
Expand Down Expand Up @@ -137,11 +136,11 @@ func (w *Watcher) syncStatus(ctx context.Context) error {
}, retryOpts...)

if err != nil {
w.status = nil
w.isSynced.Store(false)
w.cfg.Metrics.NodeSynced.WithLabelValues(w.cfg.Endpoint).Set(0)
return fmt.Errorf("failed to get node status: %w", err)
}
w.status = status
w.isSynced.Store(!status.SyncInfo.CatchingUp)
w.cfg.Metrics.NodeSynced.WithLabelValues(w.cfg.Endpoint).Set(metrics.BoolToFloat64(!status.SyncInfo.CatchingUp))
return nil
}
Expand Down

0 comments on commit 2c1e984

Please sign in to comment.