forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
70 lines (59 loc) · 1.33 KB
/
registry.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
package prospector
import (
"sync"
"github.com/elastic/beats/filebeat/harvester"
"github.com/elastic/beats/filebeat/harvester/reader"
uuid "github.com/satori/go.uuid"
)
type harvesterRegistry struct {
sync.Mutex
harvesters map[uuid.UUID]*harvester.Harvester
wg sync.WaitGroup
}
func newHarvesterRegistry() *harvesterRegistry {
return &harvesterRegistry{
harvesters: map[uuid.UUID]*harvester.Harvester{},
}
}
func (hr *harvesterRegistry) add(h *harvester.Harvester) {
hr.Lock()
defer hr.Unlock()
hr.harvesters[h.ID] = h
}
func (hr *harvesterRegistry) remove(h *harvester.Harvester) {
hr.Lock()
defer hr.Unlock()
delete(hr.harvesters, h.ID)
}
func (hr *harvesterRegistry) Stop() {
hr.Lock()
for _, hv := range hr.harvesters {
hr.wg.Add(1)
go func(h *harvester.Harvester) {
hr.wg.Done()
h.Stop()
}(hv)
}
hr.Unlock()
hr.waitForCompletion()
}
func (hr *harvesterRegistry) waitForCompletion() {
hr.wg.Wait()
}
func (hr *harvesterRegistry) start(h *harvester.Harvester, r reader.Reader) {
hr.wg.Add(1)
hr.add(h)
go func() {
defer func() {
hr.remove(h)
hr.wg.Done()
}()
// Starts harvester and picks the right type. In case type is not set, set it to default (log)
h.Harvest(r)
}()
}
func (hr *harvesterRegistry) len() uint64 {
hr.Lock()
defer hr.Unlock()
return uint64(len(hr.harvesters))
}