-
Notifications
You must be signed in to change notification settings - Fork 0
/
fanout.go
187 lines (172 loc) · 3.83 KB
/
fanout.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package gosh
import (
"errors"
"github.com/go-logr/logr"
)
// A FanOutCmd runs a set of commands in parallel, with some limit as to how many can run concurrently
type FanOutCmd struct {
Cmds []Commander
MaxConcurrency int
Log logr.Logger
sem chan struct{}
cmdChan chan Commander
errChan chan error
kill chan struct{}
mutex chan struct{}
BuilderError error
}
var (
_ = Commander(&FanOutCmd{})
)
// FanOut creates a new FanOutCmd that lets all commands run at the same time
func FanOut(cmds ...Commander) *FanOutCmd {
return &FanOutCmd{Cmds: cmds, MaxConcurrency: len(cmds)}
}
// WithLog sets a log to use for this fanout
func (f *FanOutCmd) WithLog(log logr.Logger) *FanOutCmd {
f.Log = log
return f
}
func (f *FanOutCmd) log() logr.Logger {
if f.Log.IsZero() {
return GlobalLog
}
return f.Log
}
// WithMaxConcurrency sets the maximum number of commans that can run at the same time
func (f *FanOutCmd) WithMaxConcurrency(max int) *FanOutCmd {
if max < 1 {
f.BuilderError = errors.New("Max Concurrency must be Positive")
return f
}
f.MaxConcurrency = max
return f
}
// Run implements Commander
func (f *FanOutCmd) Run() error {
if f.BuilderError != nil {
return f.BuilderError
}
err := f.Start()
if err != nil {
return err
}
err = f.Wait()
if err != nil {
return err
}
return nil
}
// Start implements Commander
func (f *FanOutCmd) Start() error {
if f.BuilderError != nil {
return f.BuilderError
}
f.cmdChan = make(chan Commander)
f.errChan = make(chan error)
f.sem = make(chan struct{})
f.kill = make(chan struct{}, 1)
f.mutex = make(chan struct{}, 1)
go func() {
for _, cmd := range f.Cmds {
f.cmdChan <- cmd
}
close(f.cmdChan)
f.log().V(DebugLogLevel).Info("all commands pushed")
}()
for ix := 0; ix < f.MaxConcurrency; ix++ {
go func() {
f.log().V(DebugLogLevel).Info("fanout started")
defer func() {
f.sem <- struct{}{}
f.log().V(DebugLogLevel).Info("sem++")
f.log().V(DebugLogLevel).Info("fanout finished")
}()
killed := false
for !killed {
func() {
var cmd Commander
var ok bool
var started bool
f.sync(func() {
select {
case cmd, ok = <-f.cmdChan:
if !ok {
killed = true
return
}
err := cmd.Start()
if err != nil {
f.log().V(DebugLogLevel).Info("Wrote err")
f.errChan <- err
return
}
started = true
case _ = <-f.kill:
f.log().V(DebugLogLevel).Info("Got kill signal, emptying cmdChan")
for range f.cmdChan {
}
killed = true
return
}
})
if killed {
return
}
if started {
f.log().V(DebugLogLevel).Info("Wrote err")
f.errChan <- cmd.Wait()
}
}()
}
}()
}
go func() {
for ix := 0; ix < f.MaxConcurrency; ix++ {
_ = <-f.sem
f.log().V(DebugLogLevel).Info("sem--")
}
f.log().V(DebugLogLevel).Info("all fanouts finished")
close(f.errChan)
}()
return nil
}
// Wait implements Commander
func (f *FanOutCmd) Wait() error {
errs := make([]error, 0, len(f.Cmds))
for err := range f.errChan {
f.log().V(DebugLogLevel).Info("Read err")
if err != nil {
errs = append(errs, err)
}
}
f.log().V(DebugLogLevel).Info("all errors recorded")
if len(errs) != 0 {
return &MultiProcessError{Errors: errs}
}
return nil
}
// Kill implements Commander
func (f *FanOutCmd) Kill() error {
errs := make([]error, 0, len(f.Cmds))
f.sync(func() {
close(f.kill)
for range f.cmdChan {
}
for _, cmd := range f.Cmds {
err := cmd.Kill()
if err != nil && !errors.Is(err, ErrNotStarted) {
errs = append(errs, err)
}
}
})
if len(errs) != 0 {
return &MultiProcessError{Errors: errs}
}
return nil
}
func (f *FanOutCmd) sync(fn func()) {
f.mutex <- struct{}{}
defer func() { _ = <-f.mutex }()
fn()
}