-
Notifications
You must be signed in to change notification settings - Fork 1
/
concurrency.go
133 lines (119 loc) · 3.14 KB
/
concurrency.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
package gofn
import (
"context"
"fmt"
"sync/atomic"
)
// ExecTasks calls ExecTasksEx with stopOnError is true
func ExecTasks(
ctx context.Context,
maxConcurrentTasks uint,
tasks ...func(ctx context.Context) error,
) error {
errMap := ExecTasksEx(ctx, maxConcurrentTasks, true, tasks...)
for _, v := range errMap {
return v
}
return nil
}
// ExecTasksEx execute multiple tasks concurrently using Go routines
// maxConcurrentTasks behaves similarly as `pool size`, pass 0 to set no limit.
// In case you want to cancel the execution, use context.WithTimeout() or context.WithCancel().
// nolint: gocognit
func ExecTasksEx(
ctx context.Context,
maxConcurrentTasks uint,
stopOnError bool,
tasks ...func(ctx context.Context) error,
) map[int]error {
taskCount := len(tasks)
if taskCount == 0 {
return nil
}
type execTaskResult struct {
Index int
Error error
}
stopped := &atomic.Value{} // NOTE: Go 1.18 has no atomic.Bool type
resultChan := make(chan *execTaskResult, taskCount)
var limiterChan chan struct{}
if maxConcurrentTasks != 0 && maxConcurrentTasks < uint(taskCount) {
limiterChan = make(chan struct{}, maxConcurrentTasks)
} else {
maxConcurrentTasks = 0
}
for i := 0; i < taskCount; i++ {
// In case we set pool size, when out of slot, this will wait until one to be available again
if maxConcurrentTasks != 0 {
limiterChan <- struct{}{}
}
go func(i int, task func(ctx context.Context) error) {
defer func() {
// In case we set pool size, release the slot when the task ends
if maxConcurrentTasks != 0 {
<-limiterChan
}
r := recover()
if r != nil && stopped.Load() == nil {
resultChan <- &execTaskResult{Index: i, Error: fmt.Errorf("%w: %v", ErrPanic, r)}
}
}()
if stopOnError && stopped.Load() != nil {
return
}
if err := ctx.Err(); err != nil {
resultChan <- &execTaskResult{Index: i, Error: err}
return
}
err := task(ctx)
if err != nil {
resultChan <- &execTaskResult{Index: i, Error: err}
} else {
resultChan <- nil
}
}(i, tasks[i])
}
errResult := map[int]error{}
for i := 0; i < taskCount; i++ {
res := <-resultChan
if res == nil {
continue
}
errResult[res.Index] = res.Error
if stopOnError {
stopped.Store(true)
break
}
}
return errResult
}
// ExecTaskFunc executes a function on every target objects
func ExecTaskFunc[T any](
ctx context.Context,
maxConcurrentTasks uint,
taskFunc func(ctx context.Context, obj T) error,
targetObjects ...T,
) error {
errMap := ExecTaskFuncEx(ctx, maxConcurrentTasks, true, taskFunc, targetObjects...)
for _, v := range errMap {
return v
}
return nil
}
// ExecTaskFuncEx executes a function on every target objects
func ExecTaskFuncEx[T any](
ctx context.Context,
maxConcurrentTasks uint,
stopOnError bool,
taskFunc func(ctx context.Context, obj T) error,
targetObjects ...T,
) map[int]error {
tasks := make([]func(ctx context.Context) error, len(targetObjects))
for i := range targetObjects {
obj := targetObjects[i]
tasks[i] = func(ctx context.Context) error {
return taskFunc(ctx, obj)
}
}
return ExecTasksEx(ctx, maxConcurrentTasks, stopOnError, tasks...)
}