/
option.go
118 lines (101 loc) · 2.4 KB
/
option.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
package client
import (
"crypto/tls"
"github.com/afex/hystrix-go/hystrix"
"github.com/plimble/kuja/broker"
"github.com/plimble/kuja/encoder"
"github.com/plimble/kuja/encoder/json"
)
var (
DefaultEncoder = json.NewEncoder()
DefaultTimeout = 1000
DefaultMaxConcurrentRequests = 0
DefaultRequestVolumeThreshold = 5
DefaultSleepWindow = 1000
DefaultErrorPercentThreshold = 50
)
type Option func(o *option)
type option struct {
Broker broker.Broker
Encoder encoder.Encoder
Header map[string]string
Timeout int
MaxConcurrentRequests int
RequestVolumeThreshold int
SleepWindow int
ErrorPercentThreshold int
ErrMaxConcurrency string
ErrCircuitOpen string
ErrTimeout string
TLSConfig *tls.Config
}
func newOption() *option {
return &option{
Encoder: DefaultEncoder,
Timeout: DefaultTimeout,
MaxConcurrentRequests: DefaultMaxConcurrentRequests,
RequestVolumeThreshold: DefaultRequestVolumeThreshold,
SleepWindow: DefaultSleepWindow,
ErrorPercentThreshold: DefaultErrorPercentThreshold,
}
}
func Broker(b broker.Broker) Option {
return func(o *option) {
o.Broker = b
}
}
func Encoder(enc encoder.Encoder) Option {
return func(o *option) {
o.Encoder = enc
}
}
func Header(hdr map[string]string) Option {
return func(o *option) {
o.Header = hdr
}
}
func Timeout(n int) Option {
return func(o *option) {
o.Timeout = n
}
}
func MaxConcurrentRequests(n int) Option {
return func(o *option) {
o.MaxConcurrentRequests = n
}
}
func RequestVolumeThreshold(n int) Option {
return func(o *option) {
o.RequestVolumeThreshold = n
}
}
func SleepWindow(n int) Option {
return func(o *option) {
o.SleepWindow = n
}
}
func ErrorPercentThreshold(n int) Option {
return func(o *option) {
o.ErrorPercentThreshold = n
}
}
func ErrMaxConcurrency(s string) Option {
return func(o *option) {
hystrix.ErrMaxConcurrency = hystrix.CircuitError{Message: s}
}
}
func ErrCircuitOpen(s string) Option {
return func(o *option) {
hystrix.ErrCircuitOpen = hystrix.CircuitError{Message: s}
}
}
func ErrTimeout(s string) Option {
return func(o *option) {
hystrix.ErrTimeout = hystrix.CircuitError{Message: s}
}
}
func TLSConfig(config *tls.Config) Option {
return func(o *option) {
o.TLSConfig = config
}
}