-
Notifications
You must be signed in to change notification settings - Fork 77
/
options.go
124 lines (109 loc) · 3.37 KB
/
options.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
package circuitbreaker
import (
"github.com/vdaas/vald/internal/errors"
"github.com/vdaas/vald/internal/timeutil"
)
type Option func(*breakerManager) error
var defaultOpts = []Option{}
func WithBreakerOpts(opts ...BreakerOption) Option {
return func(bm *breakerManager) error {
if bm.opts != nil && len(bm.opts) > 0 {
bm.opts = append(bm.opts, opts...)
} else {
bm.opts = opts
}
return nil
}
}
type BreakerOption func(*breaker) error
var defaultBreakerOpts = []BreakerOption{
WithClosedErrorRate(0.7),
WithHalfOpenErrorRate(0.5),
WithMinSamples(1000),
WithOpenTimeout("1s"),
WithClosedRefreshTimeout("10s"),
}
// WithClosedErrorRate returns an option that sets error rate when breaker state is "Closed".
// The rate is expected to be between 0 and 1.0.
// When the rate is exceeded, the breaker state will be changed from "Closed" to "Open".
func WithClosedErrorRate(f float32) BreakerOption {
return func(b *breaker) error {
if f < 0 || f > 1.0 {
return errors.NewErrInvalidOption("closedErrorRate", f)
}
b.closedErrRate = f
return nil
}
}
// WithClosedErrorTripper returns an option that sets whether it should trip when in "Closed" state.
func WithClosedErrorTripper(tp Tripper) BreakerOption {
return func(b *breaker) error {
if tp == nil {
return errors.NewErrInvalidOption("closedErrTripper", tp)
}
b.closedErrShouldTrip = tp
return nil
}
}
// WithHalfOpenErrorRate returns an option that sets error rate when breaker state is "HalfOpen".
// The rate is expected to be between 0 and 1.0.
// When the rate is exceeded, the breaker state will be changed from "HalfOpen" to "Open".
func WithHalfOpenErrorRate(f float32) BreakerOption {
return func(b *breaker) error {
if f < 0 || f > 1.0 {
return errors.NewErrInvalidOption("halfOpenErrorRate", f)
}
b.halfOpenErrRate = f
return nil
}
}
// WithHalfOpenErrorTripper returns an option that sets whether it should trip when in "Half-Open" state.
func WithHalfOpenTripper(tp Tripper) BreakerOption {
return func(b *breaker) error {
if tp == nil {
return errors.NewErrInvalidOption("halfOpenErrTripper", tp)
}
b.halfOpenErrShouldTrip = tp
return nil
}
}
// WithSamples returns an option that sets minimum sample count.
func WithMinSamples(min int64) BreakerOption {
return func(b *breaker) error {
if min < 1 {
return errors.NewErrInvalidOption("minSamples", min)
}
b.minSamples = min
return nil
}
}
// WithOpenTimeout returns an option that sets the timeout of "Open" state.
// After this period, the state will be changed from "Open" to "HalfOpen".
func WithOpenTimeout(timeout string) BreakerOption {
return func(b *breaker) error {
if len(timeout) == 0 {
return errors.NewErrInvalidOption("openTimeout", timeout)
}
d, err := timeutil.Parse(timeout)
if err != nil {
return errors.NewErrInvalidOption("openTimeout", timeout, err)
}
b.openTimeout = d
return nil
}
}
// WithClosedRefreshTimeout returns an option that sets the timeout of "Closed" state.
// After this period, the counter will be refreshed.
func WithClosedRefreshTimeout(timeout string) BreakerOption {
return func(b *breaker) error {
if len(timeout) == 0 {
return errors.NewErrInvalidOption("closedRefreshTimeout", timeout)
}
d, err := timeutil.Parse(timeout)
if err != nil {
return errors.NewErrInvalidOption("closedRefreshTimeout", timeout, err)
}
b.cloedRefreshTimeout = d
return nil
}
}