forked from gojek/heimdall
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
108 lines (92 loc) · 2.64 KB
/
Copy pathoptions.go
File metadata and controls
108 lines (92 loc) · 2.64 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
package hystrix
import (
"context"
"time"
"github.com/afex/hystrix-go/plugins"
"github.com/gojek/heimdall/v7"
"github.com/gojek/heimdall/v7/httpclient"
)
// Option represents the hystrix client options
type Option func(*Client)
// WithCommandName sets the hystrix command name
func WithCommandName(name string) Option {
return func(c *Client) {
c.hystrixCommandName = name
}
}
// WithHTTPTimeout sets timeout for http.Client
func WithHTTPTimeout(timeout time.Duration) Option {
return func(c *Client) {
httpclient.WithHTTPTimeout(timeout)(c.client)
}
}
// WithHystrixTimeout sets hystrix timeout
func WithHystrixTimeout(timeout time.Duration) Option {
return func(c *Client) {
c.hystrixTimeout = timeout
}
}
// WithMaxConcurrentRequests sets hystrix max concurrent requests
func WithMaxConcurrentRequests(maxConcurrentRequests int) Option {
return func(c *Client) {
c.maxConcurrentRequests = maxConcurrentRequests
}
}
// WithRequestVolumeThreshold sets hystrix request volume threshold
func WithRequestVolumeThreshold(requestVolumeThreshold int) Option {
return func(c *Client) {
c.requestVolumeThreshold = requestVolumeThreshold
}
}
// WithSleepWindow sets hystrix sleep window
func WithSleepWindow(sleepWindow int) Option {
return func(c *Client) {
c.sleepWindow = sleepWindow
}
}
// WithErrorPercentThreshold sets hystrix error percent threshold
func WithErrorPercentThreshold(errorPercentThreshold int) Option {
return func(c *Client) {
c.errorPercentThreshold = errorPercentThreshold
}
}
// WithFallbackFunc sets the fallback function
func WithFallbackFunc(fn fallbackFunc) Option {
if fn == nil {
return func(_ *Client) {}
}
return WithFallbackCtxFunc(func(_ context.Context, err error) error {
return fn(err)
})
}
// WithFallbackCtxFunc sets the fallback function with context support
func WithFallbackCtxFunc(fn fallbackCtxFunc) Option {
return func(c *Client) {
c.fallbackFunc = fn
}
}
// WithRetryCount sets the retry count for the Client
func WithRetryCount(retryCount int) Option {
return func(c *Client) {
c.retryCount = retryCount
}
}
// WithRetrier sets the strategy for retrying
func WithRetrier(retrier heimdall.Retriable) Option {
return func(c *Client) {
c.retrier = retrier
}
}
// WithHTTPClient sets a custom http client for hystrix client
func WithHTTPClient(client heimdall.Doer) Option {
return func(c *Client) {
opt := httpclient.WithHTTPClient(client)
opt(c.client)
}
}
// WithStatsDCollector exports hystrix metrics to a statsD backend
func WithStatsDCollector(addr, prefix string) Option {
return func(c *Client) {
c.statsD = &plugins.StatsdCollectorConfig{StatsdAddr: addr, Prefix: prefix}
}
}