-
Notifications
You must be signed in to change notification settings - Fork 0
/
pools.go
101 lines (90 loc) · 1.52 KB
/
pools.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
package pools
import (
"sync"
)
type Fn func() error
type Pools struct {
wg sync.WaitGroup
rw sync.RWMutex
fns []Fn
max int
thread int
canRun bool
stopOnError bool
}
func NewPools(max int, stopOnError bool) *Pools {
if max < 0 {
max = 1
}
return &Pools{
max: max,
stopOnError: stopOnError,
}
}
func (that *Pools) Push(fn Fn) {
that.fns = append(that.fns, fn)
}
func (that *Pools) add() {
that.rw.Lock()
defer that.rw.Unlock()
that.thread += 1
}
func (that *Pools) remove() {
that.rw.Lock()
defer that.rw.Unlock()
that.thread -= 1
}
func (that *Pools) stop() {
that.rw.Lock()
defer that.rw.Unlock()
that.canRun = false
}
func (that *Pools) start() {
that.rw.Lock()
defer that.rw.Unlock()
that.canRun = true
}
func (that *Pools) unshift() *Fn {
that.rw.Lock()
defer that.rw.Unlock()
length := len(that.fns)
if length == 0 {
return nil
}
fn := that.fns[0]
if length == 1 {
that.fns = []Fn{}
} else {
that.fns = that.fns[1:length]
}
return &fn
}
func (that *Pools) Run() error {
that.start()
var err error
for that.canRun {
if (that.stopOnError && err != nil) || len(that.fns) == 0 {
that.stop()
break
}
if len(that.fns) > 0 && that.thread <= that.max {
fn := that.unshift()
if fn != nil {
that.add()
that.wg.Add(1)
go func() {
defer func() {
that.remove()
that.wg.Done()
}()
fnErr := (*fn)()
if fnErr != nil {
err = fnErr
}
}()
}
}
}
that.wg.Wait()
return err
}