-
Notifications
You must be signed in to change notification settings - Fork 1
/
pool.go
78 lines (61 loc) · 1.25 KB
/
pool.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
71
72
73
74
75
76
77
78
package go_data_routing
import (
"sync"
)
/*
Logic of pool
1. wait for an idle worker
2. wait for an incoming job
3. on chan. close Quit all workers gracefully
*/
type PluggablePool struct {
workers []Worker
idleWorkers chan Worker
wg sync.WaitGroup
quit chan bool
input chan Exchange
results chan Exchange
nWorkers int
node *Node
}
func NewPluggablePool(nWorkers int, n *Node) *PluggablePool {
p := &PluggablePool{
idleWorkers: make(chan Worker, 100),
wg: sync.WaitGroup{},
quit: make(chan bool),
//input: make(chan Exchange),
nWorkers: nWorkers,
node: n,
}
return p
}
func (p *PluggablePool) Quit() chan bool {
return p.quit
}
func (p *PluggablePool) IdleWorkers() chan Worker {
return p.idleWorkers
}
func (p *PluggablePool) WorkerDone() {
p.wg.Done()
}
func (p *PluggablePool) spawnWorker() {
p.wg.Add(1)
w := newWorker(p)
p.workers = append(p.workers, w)
go w.run()
}
func (p *PluggablePool) joinWorkers() {
close(p.quit)
p.wg.Wait()
}
func (p *PluggablePool) FuncOnJobResult(j Exchange) {
p.node.Send(j)
}
func (p *PluggablePool) spawnWorkers() {
for i := 0; i < p.nWorkers; i++ {
p.spawnWorker()
}
}
func (p *PluggablePool) setInputChan(i chan Exchange) {
p.input = i
}