forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 2
/
worker.go
45 lines (36 loc) · 830 Bytes
/
worker.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
package common
import (
"sync"
)
// WorkerSignal ensure all events have been
// treated before closing Go routines
type WorkerSignal struct {
Done chan struct{}
wgEvent sync.WaitGroup
wgWorker sync.WaitGroup
}
func NewWorkerSignal() *WorkerSignal {
w := &WorkerSignal{}
w.Init()
return w
}
func (ws *WorkerSignal) Init() {
ws.Done = make(chan struct{})
}
func (ws *WorkerSignal) AddEvent(delta int) {
ws.wgEvent.Add(delta)
}
func (ws *WorkerSignal) DoneEvent() {
ws.wgEvent.Done()
}
func (ws *WorkerSignal) WorkerStart() {
ws.wgWorker.Add(1)
}
func (ws *WorkerSignal) WorkerFinished() {
ws.wgWorker.Done()
}
func (ws *WorkerSignal) Stop() {
ws.wgEvent.Wait() // Wait for all events to be dealt with
close(ws.Done) // Ask Go routines to exit
ws.wgWorker.Wait() // Wait for Go routines to finish
}