forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
channels.go
123 lines (105 loc) · 2.39 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
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
package beater
import (
"sync"
"sync/atomic"
"github.com/elastic/beats/filebeat/input"
"github.com/elastic/beats/filebeat/registrar"
"github.com/elastic/beats/filebeat/spooler"
)
type spoolerOutlet struct {
wg *sync.WaitGroup
done <-chan struct{}
spooler *spooler.Spooler
isOpen int32 // atomic indicator
}
type publisherChannel struct {
done chan struct{}
ch chan []*input.Event
}
type registrarLogger struct {
done chan struct{}
ch chan<- []*input.Event
}
type finishedLogger struct {
wg *sync.WaitGroup
}
func newSpoolerOutlet(
done <-chan struct{},
s *spooler.Spooler,
wg *sync.WaitGroup,
) *spoolerOutlet {
return &spoolerOutlet{
done: done,
spooler: s,
wg: wg,
isOpen: 1,
}
}
func (o *spoolerOutlet) OnEvent(event *input.Event) bool {
open := atomic.LoadInt32(&o.isOpen) == 1
if !open {
return false
}
if o.wg != nil {
o.wg.Add(1)
}
select {
case <-o.done:
if o.wg != nil {
o.wg.Done()
}
atomic.StoreInt32(&o.isOpen, 0)
return false
case o.spooler.Channel <- event:
return true
}
}
func newPublisherChannel() *publisherChannel {
return &publisherChannel{
done: make(chan struct{}),
ch: make(chan []*input.Event, 1),
}
}
func (c *publisherChannel) Close() { close(c.done) }
func (c *publisherChannel) Send(events []*input.Event) bool {
select {
case <-c.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'.
c.ch = nil
return false
case c.ch <- events:
return true
}
}
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(events []*input.Event) bool {
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
return false
case l.ch <- events:
return true
}
}
func newFinishedLogger(wg *sync.WaitGroup) *finishedLogger {
return &finishedLogger{wg}
}
func (l *finishedLogger) Published(events []*input.Event) bool {
for range events {
l.wg.Done()
}
return true
}