forked from testcontainers/testcontainers-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel.go
111 lines (92 loc) · 2.14 KB
/
parallel.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
package testcontainers
import (
"context"
"fmt"
"sync"
)
const (
MaxGenericWorkers = 8
)
type GenericParallelErrors struct {
Errors []GenericContainerRequestError
}
func (gpe GenericParallelErrors) Error() string {
return fmt.Sprintf("%v", gpe.Errors)
}
// GenericContainerRequestError represents error from parallel request
type GenericContainerRequestError struct {
Request GenericContainerRequest
Error error
}
func genericContainerRunner(
ctx context.Context,
requests <-chan GenericContainerRequest,
errors chan<- GenericContainerRequestError,
containers chan<- Container,
wg *sync.WaitGroup) {
for req := range requests {
c, err := GenericContainer(ctx, req)
if err != nil {
errors <- GenericContainerRequestError{
Request: req,
Error: err,
}
continue
}
containers <- c
}
wg.Done()
}
// GenericParallelContainers creates a generic containers with parameters in parallel mode
func GenericParallelContainers(ctx context.Context, reqs []GenericContainerRequest) ([]Container, error) {
tasksChanSize := MaxGenericWorkers
if tasksChanSize > len(reqs) {
tasksChanSize = len(reqs)
}
tasksChan := make(chan GenericContainerRequest, tasksChanSize)
errsChan := make(chan GenericContainerRequestError)
resChan := make(chan Container)
waitRes := make(chan struct{})
containers := make([]Container, 0)
errors := make([]GenericContainerRequestError, 0)
wg := sync.WaitGroup{}
wg.Add(tasksChanSize)
// run workers
for i := 0; i < tasksChanSize; i++ {
go genericContainerRunner(ctx, tasksChan, errsChan, resChan, &wg)
}
go func() {
for {
select {
case c, ok := <-resChan:
if !ok {
resChan = nil
} else {
containers = append(containers, c)
}
case e, ok := <-errsChan:
if !ok {
errsChan = nil
} else {
errors = append(errors, e)
}
}
if resChan == nil && errsChan == nil {
waitRes <- struct{}{}
break
}
}
}()
for _, req := range reqs {
tasksChan <- req
}
close(tasksChan)
wg.Wait()
close(resChan)
close(errsChan)
<-waitRes
if len(errors) == 0 {
return containers, GenericParallelErrors{Errors: errors}
}
return containers, nil
}