forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channels.go
72 lines (60 loc) · 1.38 KB
/
channels.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
package beater
import (
"sync"
"github.com/elastic/beats/filebeat/input/file"
"github.com/elastic/beats/filebeat/registrar"
"github.com/elastic/beats/libbeat/monitoring"
)
type registrarLogger struct {
done chan struct{}
ch chan<- []file.State
}
type finishedLogger struct {
wg *eventCounter
}
type eventCounter struct {
added *monitoring.Uint
done *monitoring.Uint
count *monitoring.Int
wg sync.WaitGroup
}
func newRegistrarLogger(reg *registrar.Registrar) *registrarLogger {
return ®istrarLogger{
done: make(chan struct{}),
ch: reg.Channel,
}
}
func (l *registrarLogger) Close() { close(l.done) }
func (l *registrarLogger) Published(states []file.State) {
select {
case <-l.done:
// set ch to nil, so no more events will be send after channel close signal
// has been processed the first time.
// Note: nil channels will block, so only done channel will be actively
// report 'closed'.
l.ch = nil
case l.ch <- states:
}
}
func newFinishedLogger(wg *eventCounter) *finishedLogger {
return &finishedLogger{wg}
}
func (l *finishedLogger) Published(n int) bool {
for i := 0; i < n; i++ {
l.wg.Done()
}
return true
}
func (c *eventCounter) Add(delta int) {
c.count.Add(int64(delta))
c.added.Add(uint64(delta))
c.wg.Add(delta)
}
func (c *eventCounter) Done() {
c.count.Dec()
c.done.Inc()
c.wg.Done()
}
func (c *eventCounter) Wait() {
c.wg.Wait()
}