-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbutcher.go
197 lines (174 loc) · 3.92 KB
/
butcher.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
188
189
190
191
192
193
194
195
196
197
package butcher
import (
"context"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"time"
"golang.org/x/time/rate"
)
type Butcher interface {
Run(context.Context) error
}
type butcherCfg struct {
maxWorker int
bufSize int
maxRetryTimes int
rateLimit rate.Limit
taskTimeout time.Duration
interruptSignals []os.Signal
}
type butcher[T any] struct {
*butcherCfg
executor Executor[T]
limiter *rate.Limiter
jobCh chan T
readyCh chan job[T]
generateErrCh chan error
completeCh chan struct{}
signalCh chan os.Signal
}
// NewButcher returns a butcher object for execute task executor. It has some options to control execute behaviors.
// if no options given, it runs tasks serially.
func NewButcher[T any](executor Executor[T], opts ...Option) (Butcher, error) {
b := &butcher[T]{
executor: executor,
butcherCfg: &butcherCfg{
maxWorker: 1,
bufSize: 1,
maxRetryTimes: 0,
rateLimit: rate.Inf,
taskTimeout: 0,
interruptSignals: []os.Signal{syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT},
},
}
for _, opt := range opts {
if err := opt(b.butcherCfg); err != nil {
return nil, err
}
}
b.limiter = rate.NewLimiter(b.rateLimit, b.bufSize)
b.jobCh = make(chan T, b.bufSize)
b.readyCh = make(chan job[T], b.bufSize)
b.generateErrCh = make(chan error, 1)
b.completeCh = make(chan struct{}, 1)
b.signalCh = make(chan os.Signal, 1)
return b, nil
}
// Run the task executor, return error if interrupted or GenerateJob return an error.
func (b *butcher[T]) Run(ctx context.Context) error {
if ctx == nil {
ctx = context.Background()
}
ctx, cancel := context.WithCancel(ctx)
b.generate(ctx)
b.rectify(ctx)
b.schedule(ctx)
if len(b.interruptSignals) > 0 {
signal.Notify(b.signalCh, b.interruptSignals...)
}
select {
case <-ctx.Done():
cancel()
<-b.completeCh
return fmt.Errorf("interrupted by context: %w", ctx.Err())
case s := <-b.signalCh:
cancel()
<-b.completeCh
return fmt.Errorf("interrupted by signal: %v", s)
case err := <-b.generateErrCh:
// waiting for scheduled jobs complete
<-b.completeCh
cancel()
return fmt.Errorf("generator error occurred: %w", err)
case <-b.completeCh:
cancel()
return nil
}
}
func (b *butcher[T]) generate(ctx context.Context) {
go func() {
err := safelyRun(func() error {
err := b.executor.GenerateJob(ctx, b.jobCh)
return err
})
if err != nil {
b.generateErrCh <- err
}
close(b.jobCh)
}()
}
func (b *butcher[T]) rectify(ctx context.Context) {
go func() {
for payload := range b.jobCh {
select {
case <-ctx.Done():
close(b.readyCh)
return
default:
}
_ = b.limiter.Wait(ctx)
b.readyCh <- job[T]{Type: jobTypeJob, Payload: payload}
}
close(b.readyCh)
}()
}
func (b *butcher[T]) schedule(ctx context.Context) {
var wg sync.WaitGroup
runFunc := func() {
defer wg.Done()
for j := range b.readyCh {
select {
case <-ctx.Done():
return
default:
}
var err error
for j.RetryTime <= b.maxRetryTimes {
if err = b.task(ctx, j); err != nil {
j.RetryTime++
} else {
break
}
}
b.onFinish(ctx, j, err)
}
}
go func() {
for i := 0; i < b.maxWorker; i++ {
wg.Add(1)
go runFunc()
}
wg.Wait()
b.completeCh <- struct{}{}
}()
}
func (b *butcher[T]) task(ctx context.Context, j job[T]) (err error) {
if b.taskTimeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, b.taskTimeout)
defer cancel()
}
errCh := make(chan error, 1)
go func() {
errCh <- safelyRun(func() error {
return b.executor.Task(ctx, j.Payload)
})
}()
select {
case <-ctx.Done():
err = ctx.Err()
case err = <-errCh:
}
return err
}
func (b *butcher[T]) onFinish(ctx context.Context, j job[T], err error) {
if watcher, ok := b.executor.(OnFinishWatcher[T]); ok {
_ = safelyRun(func() error {
watcher.OnFinish(ctx, j.Payload, err)
return nil
})
}
}