-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.go
More file actions
44 lines (40 loc) · 794 Bytes
/
worker.go
File metadata and controls
44 lines (40 loc) · 794 Bytes
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
package godispatch
import (
"log"
"sync"
)
// Worker has a work channel to receive work
type Worker struct {
WorkChannel chan Work
QuitChan chan bool
}
// NewWorker returns a Worker instance
func NewWorker() *Worker {
return &Worker{
WorkChannel: make(chan Work),
QuitChan: make(chan bool),
}
}
// Start receiving work from work channel and begin processing
func (w *Worker) Start(d *Dispatcher) {
var wg sync.WaitGroup
go func() {
for {
select {
case work := <-w.WorkChannel:
if work != nil {
log.Printf("Worker: Received Work %v\n", work)
wg.Add(1)
go func() {
defer wg.Done()
d.WorkHandler.Handle(work)
}()
wg.Wait()
}
case <-w.QuitChan:
log.Println("Worker: Received Quit Signal")
return
}
}
}()
}