forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
88 lines (75 loc) · 2.04 KB
/
util.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
package channel
import (
"github.com/elastic/beats/filebeat/util"
"github.com/elastic/beats/libbeat/common/atomic"
)
type subOutlet struct {
isOpen atomic.Bool
done chan struct{}
ch chan *util.Data
res chan bool
}
// SubOutlet create a sub-outlet, which can be closed individually, without closing the
// underlying outlet.
func SubOutlet(out Outleter) Outleter {
s := &subOutlet{
isOpen: atomic.MakeBool(true),
done: make(chan struct{}),
ch: make(chan *util.Data),
res: make(chan bool, 1),
}
go func() {
for event := range s.ch {
s.res <- out.OnEvent(event)
}
}()
return s
}
func (o *subOutlet) Close() error {
isOpen := o.isOpen.Swap(false)
if isOpen {
close(o.done)
}
return nil
}
func (o *subOutlet) OnEvent(d *util.Data) bool {
if !o.isOpen.Load() {
return false
}
select {
case <-o.done:
close(o.ch)
return false
case o.ch <- d:
select {
case <-o.done:
// Note: log harvester specific (leaky abstractions).
// The close at this point in time indicates an event
// already send to the publisher worker, forwarding events
// to the publisher pipeline. The harvester insists on updating the state
// (by pushing another state update to the publisher pipeline) on shutdown
// and requires most recent state update in the harvester (who can only
// update state on 'true' response).
// The state update will appear after the current event in the publisher pipeline.
// That is, by returning true here, the final state update will
// be presented to the reigstrar, after the last event being processed.
// Once all messages are in the publisher pipeline, in correct order,
// it depends on registrar/publisher pipeline if state is finally updated
// in the registrar.
close(o.ch)
return true
case ret := <-o.res:
return ret
}
}
}
// CloseOnSignal closes the outlet, once the signal triggers.
func CloseOnSignal(outlet Outleter, sig <-chan struct{}) Outleter {
if sig != nil {
go func() {
<-sig
outlet.Close()
}()
}
return outlet
}