-
Notifications
You must be signed in to change notification settings - Fork 0
/
hog.go
155 lines (129 loc) · 3.49 KB
/
hog.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
package hog
import (
"context"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)
const (
FlagHandleErrDefault FlagHandleErr = iota // Fails on first error
FlagHandleErrIgnore // Never fails on any errors
FlagHandleErrIgnoreSome // Never fails on some errors
)
type (
FlagHandleErr uint8
ProcessFn func(context.Context, Task) error
ProcessFnWithOutput func(context.Context, Task) (interface{}, error)
AdapterErrgroupFn func(context.Context, Task) func() error // AdapterFn captures errgroup context and task, and returns a closure used in errgroup.Group.Go
)
// Task represents hog unit of task.
type Task struct {
Id string
Payload interface{}
}
type ConfigErrgroup struct {
Limit bool
LimitGoroutines int
}
type Config struct {
Errgroup ConfigErrgroup
FlagHandleErr FlagHandleErr // Error handling flag
IgnoreErrs []error // Errors to ignore - if set, flag will be FlagHandleErrIgnoreSome
}
// Hog is the base for all hog functions.
// It accept AdapterFn for maximum flexibility.
// If you have a ProcessFn or ProcessFnWithOutput,
// try using adapters or use Go or GoWithOutputs instead.
func Hog(
ctx context.Context,
tasks <-chan Task,
adapter AdapterErrgroupFn,
conf Config,
) error {
tasksGroup, tasksCtx := errgroup.WithContext(ctx)
if conf.Errgroup.Limit {
tasksGroup.SetLimit(conf.Errgroup.LimitGoroutines)
}
allDone := make(chan struct{})
for {
select {
// All launched tasks are doing ok so far,
// and channel tasks has been closed
case <-allDone:
return tasksGroup.Wait()
// Some task failed (i.e. context canceled by errGroup)
case <-tasksCtx.Done():
return tasksGroup.Wait()
case task, open := <-tasks:
if !open {
go func() {
allDone <- struct{}{}
}()
continue
}
// We use errgroup's context here in capture
// to be able to cancel all goroutines on first non-nil err
tasksGroup.Go(adapter(tasksCtx, task))
}
}
}
func Go(
ctx context.Context,
tasks <-chan Task,
processFn ProcessFn,
conf Config,
) error {
return Hog(ctx, tasks, AdapterFn(processFn, conf), conf)
}
func GoWithOutputs(
ctx context.Context,
tasks <-chan Task,
outputs chan<- interface{},
processFn ProcessFnWithOutput,
conf Config,
) error {
return Hog(ctx, tasks, AdapterFnWithOutput(processFn, outputs, conf), conf)
}
func handleErrFn(conf Config) func(error) error {
switch {
case len(conf.IgnoreErrs) > 0 || conf.FlagHandleErr == FlagHandleErrIgnoreSome:
return func(err error) error {
for i := range conf.IgnoreErrs {
if errors.Is(err, conf.IgnoreErrs[i]) {
return nil
}
}
return err
}
case conf.FlagHandleErr == FlagHandleErrDefault:
return func(err error) error { return err }
case conf.FlagHandleErr == FlagHandleErrIgnore:
return func(_ error) error { return nil }
default:
return nil
}
}
func AdapterFn(processFn ProcessFn, conf Config) AdapterErrgroupFn {
handleErr := handleErrFn(conf)
return func(ctx context.Context, task Task) func() error {
return func() error {
return handleErr(processFn(ctx, task))
}
}
}
func AdapterFnWithOutput(
processFn ProcessFnWithOutput,
outputs chan<- interface{},
conf Config,
) AdapterErrgroupFn {
handleErr := handleErrFn(conf)
return func(ctx context.Context, task Task) func() error {
return func() error {
result, err := processFn(ctx, task)
if err != nil {
return errors.Wrapf(handleErr(err), "task_%s", task.Id)
}
outputs <- result
return nil
}
}
}