-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptions.go
More file actions
111 lines (97 loc) · 3.36 KB
/
options.go
File metadata and controls
111 lines (97 loc) · 3.36 KB
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
package circuit
import "time"
// Option configures a Breaker. Use the With* functions to create Options.
type Option func(*Breaker)
// WithName sets the circuit breaker's name.
// If not provided, a unique name is generated from the caller.
func WithName(name string) Option {
return func(b *Breaker) {
b.name = name
}
}
// WithTimeout sets the maximum duration that the Run func
// can execute before timing out. The default is 10 seconds.
// The runner must respect context cancellation for timeouts to take effect.
func WithTimeout(d time.Duration) Option {
return func(b *Breaker) {
b.timeout = d
}
}
// WithBackOff sets the duration that a circuit breaker is throttled.
// The default is 1 minute. The minimum is 10ms.
func WithBackOff(d time.Duration) Option {
return func(b *Breaker) {
b.backoff = d
}
}
// WithWindow sets the length of time checked for error calculation.
// The default is 5 minutes. The minimum is 10ms.
func WithWindow(d time.Duration) Option {
return func(b *Breaker) {
b.window = d
}
}
// WithThreshold sets the maximum number of errors that can occur
// within the window before the circuit breaker opens.
// By default, one error will open the circuit breaker.
func WithThreshold(n uint32) Option {
return func(b *Breaker) {
b.threshold = n
}
}
// WithLockOut sets the length of time that a circuit breaker is
// forced open before attempting to throttle. If no lockout is
// provided, the circuit breaker will transition to a throttled
// state only after its error count is at or below the threshold.
func WithLockOut(d time.Duration) Option {
return func(b *Breaker) {
b.lockout = d
}
}
// WithEstimationFunc sets the function used to determine the chance
// of a request being throttled during the backoff period.
// By default, Linear estimation is used.
func WithEstimationFunc(f EstimationFunc) Option {
return func(b *Breaker) {
b.estimate = f
}
}
// WithOpeningResetsErrors causes the error count to reset when the
// circuit breaker opens. If set, all blocked calls will come from
// the throttled backoff, unless the circuit breaker has a lockout duration.
func WithOpeningResetsErrors(v bool) Option {
return func(b *Breaker) {
b.openingResets = v
}
}
// WithMetrics sets an optional MetricsCollector for the breaker.
func WithMetrics(m MetricsCollector) Option {
return func(b *Breaker) {
b.metrics = m
}
}
// WithOnStateChange sets a callback that is invoked on every state transition.
// The callback runs synchronously after the state mutex is released, so it
// does not block other requests from evaluating state. However, it is called
// inline on the goroutine that triggered the transition.
func WithOnStateChange(fn func(breakerName string, from, to State)) Option {
return func(b *Breaker) {
b.onStateChange = fn
}
}
// WithIsSuccessful sets a callback to classify errors as successes.
// When this returns true for an error, the call is counted as a success
// and does not contribute to the error threshold (e.g., HTTP 404).
func WithIsSuccessful(fn func(error) bool) Option {
return func(b *Breaker) {
b.isSuccessful = fn
}
}
// WithIsExcluded sets a callback to exclude errors from tracking entirely.
// When this returns true, the call is not counted as success or failure
// (e.g., context.Canceled from client disconnect).
func WithIsExcluded(fn func(error) bool) Option {
return func(b *Breaker) {
b.isExcluded = fn
}
}