-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcb.go
56 lines (46 loc) · 928 Bytes
/
gcb.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
package gcb
import (
"net/http"
"time"
)
type (
// tripper
tripper struct {
http.RoundTripper
}
// Option represents an option for retry.
Option func(*Config)
Config struct {
maxRetries uint32
maxRequests uint32
interval time.Duration
timeout time.Duration
maxWait time.Duration
minWait time.Duration
readyToTrip ReadyToTrip
onStateChange OnStateChange
}
)
func NewRoundTripper(opts ...Option) *tripper {
cb := newCircuitBreaker(opts...)
t := &tripper{
RoundTripper: cb,
}
return t
}
// state
func (t *tripper) state() State {
return t.RoundTripper.(*circuit).GetState()
}
// WithMaxRetries sets the maximum maxRetries according
// to the retry policy
func WithMaxRetries(maxRetries uint32) Option {
return func(config *Config) {
config.maxRetries = maxRetries
}
}
func WithReadyToTrip(fn ReadyToTrip) Option {
return func(config *Config) {
config.readyToTrip = fn
}
}