-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparallel.go
More file actions
84 lines (74 loc) · 1.85 KB
/
parallel.go
File metadata and controls
84 lines (74 loc) · 1.85 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
package task
import (
"fmt"
"github.com/pkg/errors"
"runtime/debug"
"sync"
)
type ParallelTaskError[Key comparable] map[Key]error
func (p ParallelTaskError[Key]) Error() string {
msg := ""
for key, err := range p {
msg += fmt.Sprintf("%v: %s;", key, err)
}
return msg
}
// Task 单个任务, 指定任务的 key, 执行失败返回对应的错误
type Task[Key comparable] func(key Key) error
// TaskFunc 单个任务, 指定任务的 ID 与 key, 执行失败返回对应的错误
type TaskFunc[Key comparable] func(id int, key Key) error
// Execute 执行一个并发任务,如果有错误则一定返回 ParallelTaskError
func Execute[Key comparable](keys []Key, thread int, task Task[Key]) error {
return ParallelExecute(keys, thread, func(id int, key Key) error { return task(key) })
}
func ParallelExecute[Key comparable](keys []Key, thread int, task TaskFunc[Key]) error {
// 输入检查
if len(keys) == 0 || task == nil || thread <= 0 {
return nil
}
if thread > len(keys) {
thread = len(keys)
}
task = safeTask(task)
// 错误与 channel 的初始化
errs := make(ParallelTaskError[Key])
errMu := sync.Mutex{}
keyChan := make(chan Key, thread)
// key 生产者
go func() {
for _, key := range keys {
keyChan <- key
}
close(keyChan)
}()
// key 消费者
wg := sync.WaitGroup{}
wg.Add(thread)
for i := 0; i < thread; i++ {
go func(t int) {
for key := range keyChan {
if err := task(t, key); err != nil {
errMu.Lock()
errs[key] = err
errMu.Unlock()
}
}
wg.Done()
}(i)
}
wg.Wait()
if len(errs) == 0 {
return nil
}
return errs
}
func safeTask[Key comparable](t TaskFunc[Key]) TaskFunc[Key] {
return func(id int, key Key) (err error) {
defer func() {
if r := recover(); r != nil {
err = errors.Errorf("task %d panic: %v\nstack: %s", id, r, debug.Stack())
}
}()
return t(id, key)
}
}