-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetrics_collector.go
154 lines (129 loc) · 2.89 KB
/
metrics_collector.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package swirl
import "sync"
type localMetrics struct {
collector *metricsCollector
}
func (l localMetrics) Clients() IDList {
return variadicIdList{
count: func(list IDList) int {
return l.collector.countClients()
},
array: func(list IDList) []string {
return l.collector.getClients()
},
}
}
func (l localMetrics) Topics() IDList {
return variadicIdList{
count: func(list IDList) int {
return l.collector.countTopics()
},
array: func(list IDList) []string {
return l.collector.getTopics()
},
}
}
func (l localMetrics) Users() IDList {
return variadicIdList{
count: func(list IDList) int {
return l.collector.countUsers()
},
array: func(list IDList) []string {
return l.collector.getUsers()
},
}
}
type metricsCollector struct {
mu sync.RWMutex
appEvents AppEvents
clients, users, topics []string
}
func (m *metricsCollector) countClients() int {
m.mu.RLock()
defer m.mu.RUnlock()
return len(m.clients)
}
func (m *metricsCollector) countUsers() int {
m.mu.RLock()
defer m.mu.RUnlock()
return len(m.users)
}
func (m *metricsCollector) countTopics() int {
m.mu.RLock()
defer m.mu.RUnlock()
return len(m.topics)
}
func (m *metricsCollector) getClients() []string {
m.mu.RLock()
defer m.mu.RUnlock()
arr := make([]string, len(m.clients))
copy(arr, m.clients)
return arr
}
func (m *metricsCollector) getTopics() []string {
m.mu.RLock()
defer m.mu.RUnlock()
arr := make([]string, len(m.topics))
copy(arr, m.topics)
return arr
}
func (m *metricsCollector) getUsers() []string {
m.mu.RLock()
defer m.mu.RUnlock()
arr := make([]string, len(m.users))
copy(arr, m.users)
return arr
}
func (m *metricsCollector) close() {
m.appEvents.Close()
}
func (m *metricsCollector) get() Metrics {
return localMetrics{m}
}
func (m *metricsCollector) initBindings() {
m.appEvents.OnChange(func(app *App, log ChangeLog) {
m.mu.Lock()
defer m.mu.Unlock()
for _, clientUp := range log.ClientsUp {
m.clients = append(m.clients, clientUp)
}
for _, topicUp := range log.TopicsUp {
m.topics = append(m.topics, topicUp)
}
for _, userUp := range log.UsersUp {
m.users = append(m.users, userUp)
}
for _, clientDown := range log.ClientsDown {
m.clients = deleteFromStringArray(m.clients, clientDown)
}
for _, topicDown := range log.TopicsDown {
m.topics = deleteFromStringArray(m.topics, topicDown)
}
for _, userDown := range log.UsersDown {
m.users = deleteFromStringArray(m.users, userDown)
}
})
}
func deleteFromStringArray(arr []string, t string) []string {
for i := 0; i < len(arr); i++ {
s := arr[i]
if s == t {
arr[i] = arr[len(arr)-1]
arr = arr[:len(arr)-1]
i--
}
}
return arr
}
func newMetricsCollector(app *App) *metricsCollector {
evs := app.Events(-1)
c := &metricsCollector{
mu: sync.RWMutex{},
appEvents: evs,
clients: nil,
users: nil,
topics: nil,
}
c.initBindings()
return c
}