-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetric.go
43 lines (32 loc) · 886 Bytes
/
metric.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
// (c) 2022 Jacek Olszak
// This code is licensed under MIT license (see LICENSE for details)
package batch
import "sync"
const metricBufferSize = 1024
type metricBroker struct {
mutex sync.Mutex
subscriptions []chan Metric
}
func (s *metricBroker) subscribe() <-chan Metric {
s.mutex.Lock()
defer s.mutex.Unlock()
subscription := make(chan Metric, metricBufferSize)
s.subscriptions = append(s.subscriptions, subscription)
return subscription
}
func (s *metricBroker) publish(metric Metric) {
s.mutex.Lock()
subscriptionsCopy := make([]chan Metric, len(s.subscriptions))
copy(subscriptionsCopy, s.subscriptions)
s.mutex.Unlock()
for _, subscription := range subscriptionsCopy {
subscription <- metric
}
}
func (s *metricBroker) stop() {
s.mutex.Lock()
defer s.mutex.Unlock()
for _, subscription := range s.subscriptions {
close(subscription)
}
}