-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathpool.go
142 lines (110 loc) · 2.58 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package workerpool
import (
"fmt"
"reflect"
"sync"
)
// Pool implements worker pool
type Pool interface {
// Delegate job to a workers
// will block if channel is full, you might want to wrap it with goroutine to avoid it
// will panic if called after Stop()
Delegate(args ...interface{}) error
// AddWorker adds worker to the pool
AddWorker(fn interface{}) error
// RemoveWorker removes worker from the pool
RemoveWorker(fn interface{}) error
// WorkersNum returns number of workers in the pool
WorkersNum() int
// Stop removes all workers workers
// to resume work add them again
Stop()
}
type quitCh chan struct{}
type workers map[reflect.Value][]quitCh
type pool struct {
queue chan []reflect.Value
workers workers
mtx sync.RWMutex
}
func (p *pool) Delegate(args ...interface{}) error {
if len(p.workers) == 0 {
return fmt.Errorf("there is no workers in pool")
}
p.queue <- buildQueueValue(args)
return nil
}
func (p *pool) AddWorker(fn interface{}) error {
if err := isValidHandler(fn); err != nil {
return err
}
worker := reflect.ValueOf(fn)
p.mtx.Lock()
defer p.mtx.Unlock()
q := make(quitCh)
if _, ok := p.workers[worker]; !ok {
p.workers[worker] = []quitCh{q}
} else {
p.workers[worker] = append(p.workers[worker], q)
}
go func() {
for {
select {
case args := <-p.queue:
worker.Call(args)
case <-q:
return
}
}
}()
return nil
}
func (p *pool) RemoveWorker(fn interface{}) error {
if err := isValidHandler(fn); err != nil {
return err
}
worker := reflect.ValueOf(fn)
p.mtx.Lock()
defer p.mtx.Unlock()
if len(p.workers[worker]) > 0 {
close(p.workers[worker][len(p.workers[worker])-1])
p.workers[worker] = p.workers[worker][:len(p.workers[worker])-1]
} else {
delete(p.workers, worker)
}
return nil
}
func (p *pool) WorkersNum() int {
sum := 0
for _, qChs := range p.workers {
sum += len(qChs)
}
return sum
}
func (p *pool) Stop() {
for _, qChs := range p.workers {
for _, ch := range qChs {
close(ch)
}
}
}
func isValidHandler(fn interface{}) error {
if reflect.TypeOf(fn).Kind() != reflect.Func {
return fmt.Errorf("%s is not a reflect.Func", reflect.TypeOf(fn))
}
return nil
}
func buildQueueValue(args []interface{}) []reflect.Value {
reflectedArgs := make([]reflect.Value, 0)
for _, arg := range args {
reflectedArgs = append(reflectedArgs, reflect.ValueOf(arg))
}
return reflectedArgs
}
// New creates new worker pool with a given job queue length
func New(queueLength int) Pool {
return &pool{
queue: make(chan []reflect.Value, queueLength),
workers: make(workers),
}
}