-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflow.go
More file actions
202 lines (173 loc) · 3.72 KB
/
flow.go
File metadata and controls
202 lines (173 loc) · 3.72 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package flow
import (
"fmt"
"runtime"
"sync"
"github.com/panjf2000/ants"
)
// Silent disable all error message
var Silent = false
// ants pool (global)
var (
poolSize = 100
globalPool *ants.Pool
once sync.Once
defaultLimit = runtime.NumCPU()
)
// GoroutinePool is the goroutine pool where you want to execute your job
type GroutinePool interface {
// Submit add the function into the pool.
// it should ALWAYS returns an error when it fail to add the function into the pool.
Submit(func()) error
}
type ErrorHandler func(error)
func init() {
initGlobalPool()
if defaultLimit < 1 {
defaultLimit = 1
}
}
// initGlobalPool initializes the global ants pool
func initGlobalPool() {
once.Do(func() {
p, err := ants.NewPool(poolSize)
if err != nil {
panic(fmt.Sprintf("failed to create global ants pool: %v", err))
}
globalPool = p
})
}
// SetGlobalPoolSize sets global ants pool size (must be called before any task runs)
func SetGlobalPoolSize(size int) error {
if size < 1 {
size = 1
}
poolSize = size
if globalPool != nil {
globalPool.Release()
globalPool = nil
}
p, err := ants.NewPool(poolSize)
if err != nil {
return err
}
globalPool = p
return nil
}
// globalLimit limit all flow's concurrent work number.
// <= 0 means no limits.
var defaultPanicHandler = func(msg interface{}) {
say(msg, "panic")
}
// Flow is a sync model
type Flow struct {
jobs [][]func()
job_count int
panicHandler func(interface{})
errorHandler ErrorHandler
p GroutinePool
// concurrent limit number
limit int
runOnce *sync.Once
}
// New returns a flow instance
func New() *Flow {
return &Flow{
jobs: [][]func(){},
panicHandler: defaultPanicHandler,
limit: defaultLimit,
runOnce: new(sync.Once),
}
}
func NewWithPool(p GroutinePool) *Flow {
f := New()
f.SetGroutinePool(p)
return f
}
func NewWithLimit(limit int) *Flow {
f := New()
f.SetLimit(limit)
return f
}
func (f *Flow) SetErrorHandler(h ErrorHandler) *Flow {
f.errorHandler = h
return f
}
func (f *Flow) SetGroutinePool(p GroutinePool) *Flow {
if p != nil {
f.p = p
}
return f
}
// SetLimit set the max concurrent goroutines number
func (f *Flow) SetLimit(limit int) *Flow {
if limit < 1 {
limit = 1
}
f.limit = limit
return f
}
// With add funcs in this level
// With: run f1, run f2, run f3 ... (random execute order)
func (f *Flow) With(jobs ...func()) *Flow {
if len(f.jobs) == 0 {
f.jobs = make([][]func(), 1)
}
n := len(f.jobs)
f.jobs[n-1] = append(f.jobs[n-1], jobs...)
f.job_count += len(jobs)
return f
}
// Next add funcs in next level
// Next: wait level1(run f1, run f2, run f3...) ... wait level2(...)... (in order)
func (f *Flow) Next(jobs ...func()) *Flow {
f.jobs = append(f.jobs, jobs)
f.job_count += len(jobs)
return f
}
// Run execute these funcs
func (f *Flow) Run() {
f.runOnce.Do(func() {
if f.job_count == 0 || len(f.jobs) == 0 {
return
}
for _, jobs := range f.jobs {
wg := new(sync.WaitGroup)
sem := make(chan struct{}, f.limit) // Per-level concurrency limit
for _, job := range jobs {
j := job
wg.Add(1)
sem <- struct{}{} // block if over limit
p := f.p
if p == nil {
p = globalPool
}
err := p.Submit(func() {
defer func() {
if msg := recover(); msg != nil {
f.panicHandler(msg)
}
<-sem // release slot
wg.Done()
}()
j()
})
if err != nil && f.errorHandler != nil {
f.errorHandler(err)
}
}
wg.Wait()
}
})
}
// OnPanic set panicHandler
func (f *Flow) OnPanic(panicHandler func(interface{})) *Flow {
f.panicHandler = panicHandler
return f
}
func say(msg interface{}, level string) {
if Silent {
return
}
fmt.Printf("%s %s: %v\n", "flow", level, msg)
}