-
-
Notifications
You must be signed in to change notification settings - Fork 157
/
config.go
85 lines (75 loc) · 2.11 KB
/
config.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
package sr
import (
"crypto/tls"
"net"
"net/http"
"strings"
"time"
)
type (
// Opt is an option to configure a client.
Opt interface{ apply(*Client) }
opt struct{ fn func(*Client) }
)
func (o opt) apply(cl *Client) { o.fn(cl) }
// HTTPClient sets the http client that the schema registry client uses,
// overriding the default client that speaks plaintext with a timeout of 5s.
func HTTPClient(httpcl *http.Client) Opt {
return opt{func(cl *Client) { cl.httpcl = httpcl }}
}
// UserAgent sets the User-Agent to use in requests, overriding the default
// "franz-go".
func UserAgent(ua string) Opt {
return opt{func(cl *Client) { cl.ua = ua }}
}
// URLs sets the URLs that the client speaks to, overriding the default
// http://localhost:8081. This option automatically prefixes any URL that is
// missing an http:// or https:// prefix with http://.
func URLs(urls ...string) Opt {
return opt{func(cl *Client) {
for i, u := range urls {
if strings.HasPrefix(u, "http://") || strings.HasPrefix(u, "https://") {
continue
}
urls[i] = "http://" + u
}
cl.urls = urls
}}
}
// DialTLSConfig sets a tls.Config to use in the default http client.
func DialTLSConfig(c *tls.Config) Opt {
return opt{func(cl *Client) {
cl.httpcl = &http.Client{
Timeout: 5 * time.Second,
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
TLSClientConfig: c,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}
}}
}
// BasicAuth sets basic authorization to use for every request.
func BasicAuth(user, pass string) Opt {
return opt{func(cl *Client) {
cl.basicAuth = &struct {
user string
pass string
}{user, pass}
}}
}
// DefaultParams sets default parameters to apply to every request.
func DefaultParams(ps ...Param) Opt {
return opt{func(cl *Client) {
cl.defParams = mergeParams(ps...)
}}
}