-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
56 lines (50 loc) · 1.21 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
package executor
import "time"
const TIMEOUT = 3 * time.Second
type Worker struct {
id string
jobchan chan Job
quit chan bool
handler func(string, interface{})
njob uint // total jobs
ndone uint // total done jobs
mgr *Executor
}
// NewWorker creates, and returns a new Worker object. Its only argument
// is a channel that the worker can add itself to whenever it is done its
// work.
func NewWorker(id string, handler func(string, interface{}), mgr *Executor) *Worker {
return &Worker{
id: id,
jobchan: make(chan Job, 20),
quit: make(chan bool),
handler: handler,
mgr: mgr,
}
}
// start runs the worker by starting a goroutine, that is
// an infinite "for-select" loop.
func (w *Worker) start() {
loop:
for {
select {
case job := <-w.jobchan:
w.njob++
w.handler(job.key, job.data)
w.ndone++
case <-w.quit:
break loop
case <-time.After(TIMEOUT):
w.mgr.Lock()
if len(w.jobchan) == 0 {
w.mgr.removeWorker(w.id)
w.mgr.Unlock()
break loop
}
w.mgr.Unlock()
}
}
}
// stop tells the worker to stop listening for work requests.
// Note that the worker will only stop *after* it has finished its work.
func (w *Worker) stop() { w.quit <- true }