-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathaggregator.gen.go
106 lines (82 loc) · 2.88 KB
/
aggregator.gen.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
// Code generated by circuitgen tool. DO NOT EDIT
package circuittestmulti
import (
"context"
"github.com/cep21/circuit"
"github.com/twitchtv/circuitgen/internal/circuitgentest"
)
// CircuitWrapperAggregatorConfig contains configuration for CircuitWrapperAggregator. All fields are optional
type CircuitWrapperAggregatorConfig struct {
// ShouldSkipError determines whether an error should be skipped and have the circuit
// track the call as successful. This takes precedence over IsBadRequest
ShouldSkipError func(error) bool
// IsBadRequest is an optional bad request checker. It is useful to not count user errors as faults
IsBadRequest func(error) bool
// Prefix is prepended to all circuit names
Prefix string
// Defaults are used for all created circuits. Per-circuit configs override this
Defaults circuit.Config
// CircuitIncSum is the configuration used for the IncSum circuit. This overrides values set by Defaults
CircuitIncSum circuit.Config
}
// CircuitWrapperAggregator is a circuit wrapper for *circuitgentest.Aggregator
type CircuitWrapperAggregator struct {
*circuitgentest.Aggregator
// ShouldSkipError determines whether an error should be skipped and have the circuit
// track the call as successful. This takes precedence over IsBadRequest
ShouldSkipError func(error) bool
// IsBadRequest checks whether to count a user error against the circuit. It is recommended to set this
IsBadRequest func(error) bool
// CircuitIncSum is the circuit for method IncSum
CircuitIncSum *circuit.Circuit
}
// NewCircuitWrapperAggregator creates a new circuit wrapper and initializes circuits
func NewCircuitWrapperAggregator(
manager *circuit.Manager,
embedded *circuitgentest.Aggregator,
conf CircuitWrapperAggregatorConfig,
) (*CircuitWrapperAggregator, error) {
if conf.ShouldSkipError == nil {
conf.ShouldSkipError = func(err error) bool {
return false
}
}
if conf.IsBadRequest == nil {
conf.IsBadRequest = func(err error) bool {
return false
}
}
w := &CircuitWrapperAggregator{
Aggregator: embedded,
ShouldSkipError: conf.ShouldSkipError,
IsBadRequest: conf.IsBadRequest,
}
var err error
w.CircuitIncSum, err = manager.CreateCircuit(conf.Prefix+"Aggregator.IncSum", conf.CircuitIncSum, conf.Defaults)
if err != nil {
return nil, err
}
return w, nil
}
// IncSum calls the embedded *circuitgentest.Aggregator's method IncSum with CircuitIncSum
func (w *CircuitWrapperAggregator) IncSum(ctx context.Context, p1 int) error {
var skippedErr error
err := w.CircuitIncSum.Run(ctx, func(ctx context.Context) error {
err := w.Aggregator.IncSum(ctx, p1)
if w.ShouldSkipError(err) {
skippedErr = err
return nil
}
if w.IsBadRequest(err) {
return &circuit.SimpleBadRequest{Err: err}
}
return err
})
if skippedErr != nil {
err = skippedErr
}
if berr, ok := err.(*circuit.SimpleBadRequest); ok {
err = berr.Err
}
return err
}