-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontroller_run.go
69 lines (59 loc) · 1.65 KB
/
controller_run.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
59
60
61
62
63
64
65
66
67
68
69
package p2p
import "time"
// functions related to the timed run loop
// run is responsible for everything that involves proactive management
// not based on reactions. runs once a second
func (c *controller) run() {
c.logger.Debug("Start run()")
defer c.logger.Error("Stop run()")
for {
c.runCatRound()
c.runMetrics()
c.runPing()
select {
case <-c.net.stopper:
return
case <-time.After(time.Second):
}
}
}
// the factom network ping behavior is to send a ping message after
// a specific duration has passed
func (c *controller) runPing() {
for _, p := range c.peers.Slice() {
if p.LastSendAge() > c.net.conf.PingInterval {
ping := newParcel(TypePing, []byte("Ping"))
p.Send(ping)
}
}
}
func (c *controller) makeMetrics() map[string]PeerMetrics {
metrics := make(map[string]PeerMetrics)
for _, p := range c.peers.Slice() {
metrics[p.Hash] = p.GetMetrics()
}
return metrics
}
func (c *controller) runMetrics() {
metrics := c.makeMetrics()
if c.net.metricsHook != nil {
go c.net.metricsHook(metrics)
}
if c.net.prom != nil {
var MPSDown, MPSUp, BPSDown, BPSUp float64
for _, m := range metrics {
MPSDown += float64(m.MPSDown)
MPSUp += float64(m.MPSUp)
BPSDown += float64(m.BPSDown)
BPSUp += float64(m.BPSUp)
}
c.net.prom.ByteRateDown.Set(BPSDown)
c.net.prom.ByteRateUp.Set(BPSUp)
c.net.prom.MessageRateUp.Set(MPSUp)
c.net.prom.MessageRateDown.Set(MPSDown)
c.net.prom.ToNetwork.Set(float64(len(c.net.toNetwork)))
c.net.prom.ToNetworkRatio.Set(c.net.toNetwork.FillRatio())
c.net.prom.FromNetwork.Set(float64(len(c.net.fromNetwork)))
c.net.prom.FromNetworkRatio.Set(c.net.fromNetwork.FillRatio())
}
}