-
Notifications
You must be signed in to change notification settings - Fork 85
/
options.go
119 lines (105 loc) · 2.82 KB
/
options.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
119
package meilisearch
import (
"crypto/tls"
"net"
"net/http"
"time"
)
var (
defaultMeiliOpt = &meiliOpt{
client: &http.Client{
Transport: baseTransport(),
},
contentEncoding: &encodingOpt{
level: DefaultCompression,
},
retryOnStatus: map[int]bool{
502: true,
503: true,
504: true,
},
disableRetry: false,
maxRetries: 3,
}
)
type meiliOpt struct {
client *http.Client
apiKey string
contentEncoding *encodingOpt
retryOnStatus map[int]bool
disableRetry bool
maxRetries uint8
}
type encodingOpt struct {
encodingType ContentEncoding
level EncodingCompressionLevel
}
type Option func(*meiliOpt)
// WithCustomClient set custom http.Client
func WithCustomClient(client *http.Client) Option {
return func(opt *meiliOpt) {
opt.client = client
}
}
// WithCustomClientWithTLS client support tls configuration
func WithCustomClientWithTLS(tlsConfig *tls.Config) Option {
return func(opt *meiliOpt) {
trans := baseTransport()
trans.TLSClientConfig = tlsConfig
opt.client = &http.Client{Transport: trans}
}
}
// WithAPIKey is API key or master key.
//
// more: https://www.meilisearch.com/docs/reference/api/keys
func WithAPIKey(key string) Option {
return func(opt *meiliOpt) {
opt.apiKey = key
}
}
// WithContentEncoding support the Content-Encoding header indicates the media type is compressed by a given algorithm.
// compression improves transfer speed and reduces bandwidth consumption by sending and receiving smaller payloads.
// the Accept-Encoding header, instead, indicates the compression algorithm the client understands.
//
// more: https://www.meilisearch.com/docs/reference/api/overview#content-encoding
func WithContentEncoding(encodingType ContentEncoding, level EncodingCompressionLevel) Option {
return func(opt *meiliOpt) {
opt.contentEncoding = &encodingOpt{
encodingType: encodingType,
level: level,
}
}
}
// WithCustomRetries set retry on specific http error code and max retries (min: 1, max: 255)
func WithCustomRetries(retryOnStatus []int, maxRetries uint8) Option {
return func(opt *meiliOpt) {
opt.retryOnStatus = make(map[int]bool)
for _, status := range retryOnStatus {
opt.retryOnStatus[status] = true
}
if maxRetries == 0 {
maxRetries = 1
}
opt.maxRetries = maxRetries
}
}
// DisableRetries disable retry logic in client
func DisableRetries() Option {
return func(opt *meiliOpt) {
opt.disableRetry = true
}
}
func baseTransport() *http.Transport {
return &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
}