-
Notifications
You must be signed in to change notification settings - Fork 477
/
Copy pathbatch.go
78 lines (66 loc) · 1.38 KB
/
batch.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 pool
import (
v3 "gopkg.in/go-playground/pool.v3"
)
// 批量执行线程池
type BatchGoPool struct {
pool v3.Pool
batch v3.Batch
results chan BatchResult
}
type BatchResult struct {
Value interface{}
Error error
}
func NewBatchPool(workers uint) BatchGoPool {
var pool v3.Pool
if workers == 0 {
pool = v3.New()
} else {
pool = v3.NewLimited(workers)
}
batch := pool.Batch()
return BatchGoPool{
pool: pool,
batch: batch,
results: make(chan BatchResult),
}
}
func (b *BatchGoPool) Queue(fn func() (interface{}, error)) {
workFn := func(wu v3.WorkUnit) (interface{}, error) {
if wu.IsCancelled() {
return nil, nil
}
return fn()
}
b.batch.Queue(workFn)
}
func (b *BatchGoPool) QueueWithArgs(fn func(args ...interface{}) (interface{}, error), args ...interface{}) {
workFn := func(wu v3.WorkUnit) (interface{}, error) {
if wu.IsCancelled() {
return nil, nil
}
return fn(args...)
}
b.batch.Queue(workFn)
}
func (b *BatchGoPool) Results() <-chan BatchResult {
go func(bp *BatchGoPool) {
for result := range bp.batch.Results() {
err := result.Error()
value := result.Value()
bp.results <- BatchResult{Value: value, Error: err}
}
close(bp.results)
}(b)
return b.results
}
func (b *BatchGoPool) Cancel() {
b.batch.Cancel()
}
func (b *BatchGoPool) WaitAll() {
b.batch.WaitAll()
}
func (b *BatchGoPool) Close() {
b.pool.Close()
}