/
registry.go
53 lines (42 loc) · 1.33 KB
/
registry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package p2p
import (
"sync"
gossipv1 "github.com/certusone/wormhole/node/pkg/proto/gossip/v1"
"github.com/wormhole-foundation/wormhole/sdk/vaa"
)
// The p2p package implements a simple global metrics registry singleton for node status values transmitted on-chain.
type registry struct {
mu sync.Mutex
// Mapping of chain IDs to network status messages.
networkStats map[vaa.ChainID]*gossipv1.Heartbeat_Network
// Per-chain error counters
errorCounters map[vaa.ChainID]uint64
errorCounterMu sync.Mutex
}
func NewRegistry() *registry {
return ®istry{
networkStats: map[vaa.ChainID]*gossipv1.Heartbeat_Network{},
errorCounters: map[vaa.ChainID]uint64{},
}
}
var (
DefaultRegistry = NewRegistry()
)
// SetNetworkStats sets the current network status to be broadcast in Heartbeat messages.
// The "Id" field is automatically set to the specified chain ID.
func (r *registry) SetNetworkStats(chain vaa.ChainID, data *gossipv1.Heartbeat_Network) {
r.mu.Lock()
data.Id = uint32(chain)
r.networkStats[chain] = data
r.mu.Unlock()
}
func (r *registry) AddErrorCount(chain vaa.ChainID, delta uint64) {
r.errorCounterMu.Lock()
defer r.errorCounterMu.Unlock()
r.errorCounters[chain] += 1
}
func (r *registry) GetErrorCount(chain vaa.ChainID) uint64 {
r.errorCounterMu.Lock()
defer r.errorCounterMu.Unlock()
return r.errorCounters[chain]
}