-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
108 lines (92 loc) · 2.24 KB
/
run.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
package db
import (
"context"
"sync"
)
// A type which runs a query on a given connection.
type Runnable interface {
Run(ctx context.Context, able Queryable) error
}
// A runner is a function which processes many runnable interface
type Runner func(many []Runnable, ctx context.Context, able Queryable) error
// Runs a list of queries sequentially
func RunSequential(many []Runnable, ctx context.Context, able Queryable) error {
for _, run := range many {
err := run.Run(ctx, able)
if err != nil {
return err
}
}
return nil
}
var _ Runner = RunSequential
// Runs a list of queries at the same time. If any of them errors it will
// try to stop all other queries and return as soon as possible.
func RunConcurrently(many []Runnable, ctx context.Context, able Queryable) error {
cancelCtx, cancel := context.WithCancel(ctx)
lastError := make(chan error)
group := sync.WaitGroup{}
for i := range many {
group.Add(1)
go func(run Runnable) {
defer group.Done()
err := run.Run(cancelCtx, able)
if err != nil {
cancel()
lastError <- err
close(lastError)
}
}(many[i])
}
group.Wait()
cancel()
select {
case err := <-lastError:
return err
default:
}
return nil
}
var _ Runner = RunConcurrently
// Creates a pooled runner
func NewPooledRunner(poolSize int) Runner {
return func(many []Runnable, ctx context.Context, able Queryable) error {
return RunPooled(many, ctx, able, poolSize)
}
}
// Runs a list of queries at the same time. If any of them errors it will
// try to stop all other queries and return as soon as possible.
func RunPooled(many []Runnable, ctx context.Context, able Queryable, poolSize int) error {
cancelCtx, cancel := context.WithCancel(ctx)
lastError := make(chan error)
group := sync.WaitGroup{}
jobs := make(chan struct{}, poolSize)
for i := 0; i < poolSize; i++ {
jobs <- struct{}{}
}
for i := range many {
group.Add(1)
go func(run Runnable) {
defer group.Done()
<-jobs
err := run.Run(cancelCtx, able)
if err != nil {
cancel()
lastError <- err
close(lastError)
} else {
jobs <- struct{}{}
}
}(many[i])
}
group.Wait()
close(jobs)
cancel()
select {
case err := <-lastError:
return err
default:
}
return nil
}
var _ Runner = NewPooledRunner(10)