/
work.go
70 lines (60 loc) · 1.56 KB
/
work.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 yenc
// Content .
type Content []byte
// Bytes of content
func (c Content) Bytes() []byte {
return []byte(c)
}
// Data structure which holds yEnc data to be decoded by Decoder
type Data struct {
Content Content
Meta *Meta
Error error
}
type worker struct {
id int
workQueue chan *Data // Queue to get work which needs to be processed
respChan chan *Data // Response Channel for completed work
pool chan chan *Data // Worker pool
quit chan bool //quit channel to shutdown the worker
}
func (w *worker) start() {
go func() {
for {
// Add worker data queue to
// the worker pool
w.pool <- w.workQueue
select {
case work := <- w.workQueue:
// Process incoming work and return over response channel
w.respChan <- DecodeData(work)
case <- w.quit:
// Stop work
return
}
}
}()
}
func (w *worker) stop() {
go func() {
// Stop worker go routine
w.quit <- true
// Close response channel
close(w.respChan)
}()
}
func (w *worker) responseChannel() chan *Data {
return w.respChan
}
// Defined within work.go because it's easy to mis
// changes made to worker struct
func (d *Decoder) newWorker(id int, pool chan chan *Data) *worker {
return &worker {
id: id,
workQueue: make(chan *Data),
respChan: make(chan *Data),
pool: pool,
quit: make(chan bool),
}
}
// EOF