-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathoption.go
98 lines (82 loc) · 2.57 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
package eureka
import (
"crypto/tls"
"net"
"net/http"
"time"
"golang.org/x/oauth2/clientcredentials"
"github.com/st3v/go-eureka/retry"
)
var (
// DefaultRetrySelector defines the default selector to be used when selecting
// endpoints for request retries.
DefaultRetrySelector retry.Selector = retry.RoundRobin
// DefaultRetryLimit defines the default allowance for request retries.
DefaultRetryLimit retry.Allow = retry.MaxRetries(3)
// DefaultRetryDelay defines the default delay in-between request retries.
DefaultRetryDelay retry.Delay = retry.ConstantDelay(1 * time.Second)
// DefaultTransport defines the default roundtripper used by the internal http client.
DefaultTransport = &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
KeepAlive: 60 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
ResponseHeaderTimeout: 5 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: 1,
}
// DefaultTimeout defines the default timeout used by the internal http client.
DefaultTimeout = 10 * time.Second
)
// Option can be used to configure a Client.
type Option func(*Client)
// HTTPTimeout sets the timeout for the internal HTTP client.
func HTTPTimeout(t time.Duration) Option {
return func(c *Client) {
c.timeout = t
}
}
// HTTPTransport sets the transport for the internal HTTP client.
func HTTPTransport(t *http.Transport) Option {
return func(c *Client) {
c.transport = t
}
}
// TLSConfig sets the TLS config for the internal HTTP client.
func TLSConfig(config *tls.Config) Option {
return func(c *Client) {
c.tlsConfig = config
}
}
// Oauth2ClientCredentials instructs the internal http client to use the
// Oauth2 Client Credential flow to authenticate with the Eureka server.
func Oauth2ClientCredentials(clientID, clientSecret, tokenURI string, scopes ...string) Option {
return func(c *Client) {
c.oauth2Config = &clientcredentials.Config{
ClientID: clientID,
ClientSecret: clientSecret,
TokenURL: tokenURI,
Scopes: scopes,
}
}
}
// RetryLimit instructs the client to limit retries to a given allowance.
func RetryLimit(limit retry.Allow) Option {
return func(c *Client) {
c.retryLimit = limit
}
}
// RetrySelector instructs the client to use a given selector to pick endpoints
// for retries.
func RetrySelector(selector retry.Selector) Option {
return func(c *Client) {
c.retrySelector = selector
}
}
// RetryDelay sets the delau the client in-between request retries.
func RetryDelay(delay retry.Delay) Option {
return func(c *Client) {
c.retryDelay = delay
}
}