-
Notifications
You must be signed in to change notification settings - Fork 192
/
relays_monitor_aggregator.go
62 lines (53 loc) · 1.56 KB
/
relays_monitor_aggregator.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
package metrics
import (
"context"
"sync"
"time"
)
type HealthCheckUpdatable interface {
UpdateHealthCheckStatus(status bool)
}
type RelaysMonitorAggregator struct {
relaysMonitors map[string]*RelaysMonitor // key is endpoint: chainID+apiInterface
ticker *time.Ticker
healthCheckUpdatable HealthCheckUpdatable
lock sync.RWMutex
}
func NewRelaysMonitorAggregator(interval time.Duration, rpcConsumerLogs HealthCheckUpdatable) *RelaysMonitorAggregator {
return &RelaysMonitorAggregator{
relaysMonitors: map[string]*RelaysMonitor{},
ticker: time.NewTicker(interval),
healthCheckUpdatable: rpcConsumerLogs,
lock: sync.RWMutex{},
}
}
func (rma *RelaysMonitorAggregator) RegisterRelaysMonitor(rpcEndpointKey string, relaysMonitor *RelaysMonitor) {
rma.lock.Lock()
defer rma.lock.Unlock()
rma.relaysMonitors[rpcEndpointKey] = relaysMonitor
}
func (rma *RelaysMonitorAggregator) StartMonitoring(ctx context.Context) {
go func() {
for {
select {
case <-rma.ticker.C:
go rma.runHealthCheck()
case <-ctx.Done():
rma.ticker.Stop()
return
}
}
}()
}
func (rma *RelaysMonitorAggregator) runHealthCheck() {
rma.lock.RLock()
defer rma.lock.RUnlock()
// If at least one of the relays monitors is healthy, we set the status to TRUE, otherwise we set it to FALSE.
for _, relaysMonitor := range rma.relaysMonitors {
if relaysMonitor.IsHealthy() {
rma.healthCheckUpdatable.UpdateHealthCheckStatus(true)
return
}
}
rma.healthCheckUpdatable.UpdateHealthCheckStatus(false)
}