Skip to content

Commit

Permalink
node/pkg/p2p: use mutex for error counters
Browse files Browse the repository at this point in the history
Change-Id: Idde862e034c567b7ac2d5648bec0b3505f032b0c
  • Loading branch information
Leo committed Aug 10, 2021
1 parent 212e04a commit 3f81840
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions bridge/pkg/p2p/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
gossipv1 "github.com/certusone/wormhole/bridge/pkg/proto/gossip/v1"
"github.com/certusone/wormhole/bridge/pkg/vaa"
"sync"
"sync/atomic"
)

// The p2p package implements a simple global metrics registry singleton for node status values transmitted on-chain.
Expand All @@ -15,8 +14,9 @@ type registry struct {
// Mapping of chain IDs to network status messages.
networkStats map[vaa.ChainID]*gossipv1.Heartbeat_Network

// Atomic per-chain error counters
errorCounters map[vaa.ChainID]uint64
// Per-chain error counters
errorCounters map[vaa.ChainID]uint64
errorCounterMu sync.Mutex

// Value of Heartbeat.guardian_addr.
guardianAddress string
Expand Down Expand Up @@ -51,11 +51,13 @@ func (r *registry) SetNetworkStats(chain vaa.ChainID, data *gossipv1.Heartbeat_N
}

func (r *registry) AddErrorCount(chain vaa.ChainID, delta uint64) {
ctr := r.errorCounters[chain]
atomic.AddUint64(&ctr, delta)
r.errorCounterMu.Lock()
defer r.errorCounterMu.Unlock()
r.errorCounters[chain] += 1
}

func (r *registry) GetErrorCount(chain vaa.ChainID) uint64 {
ctr := r.errorCounters[chain]
return atomic.LoadUint64(&ctr)
r.errorCounterMu.Lock()
defer r.errorCounterMu.Unlock()
return r.errorCounters[chain]
}

0 comments on commit 3f81840

Please sign in to comment.