-
Notifications
You must be signed in to change notification settings - Fork 12
/
gossip.go
58 lines (48 loc) · 1.52 KB
/
gossip.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
54
55
56
57
58
package dashboardmetrics
import (
"math"
"sync"
)
var (
lastGossipMetricsLock = &sync.RWMutex{}
lastGossipMetrics = &GossipMetrics{
Incoming: 0,
New: 0,
Outgoing: 0,
}
lastIncomingBlocksCount uint32
lastIncomingNewBlocksCount uint32
lastOutgoingBlocksCount uint32
)
// uint32Diff returns the difference between newCount and oldCount
// and catches overflows.
func uint32Diff(newCount uint32, oldCount uint32) uint32 {
// Catch overflows
if newCount < oldCount {
return (math.MaxUint32 - oldCount) + newCount
}
return newCount - oldCount
}
// measureGossipMetrics measures the BPS values.
func measureGossipMetrics() {
newIncomingBlocksCount := deps.P2PMetrics.IncomingBlocks.Load()
newIncomingNewBlocksCount := deps.P2PMetrics.IncomingNewBlocks.Load()
newOutgoingBlocksCount := deps.P2PMetrics.OutgoingBlocks.Load()
// calculate the new BPS metrics
lastGossipMetricsLock.Lock()
defer lastGossipMetricsLock.Unlock()
lastGossipMetrics = &GossipMetrics{
Incoming: uint32Diff(newIncomingBlocksCount, lastIncomingBlocksCount),
New: uint32Diff(newIncomingNewBlocksCount, lastIncomingNewBlocksCount),
Outgoing: uint32Diff(newOutgoingBlocksCount, lastOutgoingBlocksCount),
}
// store the new counters
lastIncomingBlocksCount = newIncomingBlocksCount
lastIncomingNewBlocksCount = newIncomingNewBlocksCount
lastOutgoingBlocksCount = newOutgoingBlocksCount
}
func gossipMetrics() *GossipMetrics {
lastGossipMetricsLock.RLock()
defer lastGossipMetricsLock.RUnlock()
return lastGossipMetrics
}