/
pool.go
197 lines (167 loc) · 4.36 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
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
package portal
import (
"context"
"fmt"
"runtime"
"sync"
"github.com/panjf2000/ants/v2"
"github.com/pkg/errors"
)
var (
// maxWorkerPoolSize is default to 10k.
// Since the number of incoming requests are unknown,
// we must limit the spawned goroutines to avoid
// consuming too many resources.
maxWorkerPoolSize = 10 * 1000
// levelWorkerPoolMap are global goroutine pools which are
// responsible for processing schema fields asynchronously.
// Note that each dumping level gets a worker pool to avoid
// dead lock.
levelWorkerPoolMap sync.Map
)
var (
errFailedToInitWorkerPool = errors.New("failed to init portal worker pool")
)
type (
// processFunc is a callback function to be called in a worker.
// It accepts user defined payload and returns user expected result.
processFunc func(payload interface{}) (interface{}, error)
jobRequest struct {
ctx context.Context
wg *sync.WaitGroup
payload interface{}
pf processFunc
resultChan chan *jobResult
}
// jobResult contains the result data and an optional error.
jobResult struct {
Data interface{}
Err error
}
)
// submitJobs submits jobs to the worker pool and return the collected results.
func submitJobs(ctx context.Context, pf processFunc, payloads ...interface{}) (<-chan *jobResult, error) {
logger.Debugf("[portal.pool] submit jobs with %d payloads", len(payloads))
var wg sync.WaitGroup
ctx, cancel := context.WithCancel(ctx)
defer cancel()
level := dumpDepthFromContext(ctx)
var workerPool *ants.PoolWithFunc
pool, ok := levelWorkerPoolMap.Load(level)
if ok {
workerPool = pool.(*ants.PoolWithFunc)
} else {
logger.Debugf("[portal.pool] worker pool with level %d not found, try to create a new one", level)
pool, err := ants.NewPoolWithFunc(1, processRequest)
if err != nil {
return nil, errors.WithStack(err)
}
levelWorkerPoolMap.Store(level, pool)
workerPool = pool
SetMaxPoolSize(maxWorkerPoolSize)
}
resultChan := make(chan *jobResult, len(payloads))
for _, payload := range payloads {
wg.Add(1)
err := workerPool.Invoke(&jobRequest{
ctx: ctx,
wg: &wg,
payload: payload,
pf: pf,
resultChan: resultChan,
})
if err != nil {
cancel()
return nil, errors.WithStack(errFailedToInitWorkerPool)
}
}
go func() {
wg.Wait()
close(resultChan)
}()
results := make(chan *jobResult, len(payloads))
for result := range resultChan {
if result.Err != nil {
cancel()
}
results <- result
}
close(results)
return results, nil
}
// SetMaxPoolSize limits the capacity of all worker pools.
func SetMaxPoolSize(size int) {
logger.Debugf("[portal.pool] set max worker pool size to %d", size)
if size == 0 {
maxWorkerPoolSize = 1
}
maxWorkerPoolSize = size
var length int
levelWorkerPoolMap.Range(func(key, value interface{}) bool {
length++
return true
})
if length == 0 {
return
}
// make sure capacity is valid.
capacity := size / length
if capacity == 0 {
capacity = 1
}
levelWorkerPoolMap.Range(func(level, value interface{}) bool {
pool, ok := value.(*ants.PoolWithFunc)
if ok {
logger.Debugf("[portal.pool] tune pool.%d capacity to %d", level.(int), capacity)
pool.Tune(capacity)
}
return true
})
}
// CleanUp releases the global worker pool.
// You should call this function only once before the main goroutine exits.
func CleanUp() {
levelWorkerPoolMap.Range(func(key, value interface{}) bool {
pool, ok := value.(*ants.PoolWithFunc)
if ok {
pool.Release()
}
return true
})
}
func processRequest(request interface{}) {
switch req := request.(type) {
case *jobRequest:
defer req.wg.Done()
select {
case <-req.ctx.Done():
case req.resultChan <- func() *jobResult {
data, err := func() (data interface{}, err error) {
defer func() {
if p := recover(); p != nil {
var buf [4096]byte
n := runtime.Stack(buf[:], false)
err = errors.New(fmt.Sprintf("%s", p))
logger.Errorf("[portal.pool] worker crashed: %s\n%s\n", p, buf[:n])
}
}()
data, err = req.pf(req.payload)
return
}()
return &jobResult{Data: data, Err: err}
}():
}
default:
logger.Warnf("[portal.pool] invalid worker request: '%s'", request)
}
}
func init() {
p, err := ants.NewPoolWithFunc(
maxWorkerPoolSize,
processRequest,
)
if err != nil {
panic(errFailedToInitWorkerPool)
}
levelWorkerPoolMap.Store(0, p)
}