-
Notifications
You must be signed in to change notification settings - Fork 86
/
client.go
344 lines (291 loc) · 8.04 KB
/
client.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package meilisearch
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"sync"
"time"
)
type client struct {
client *http.Client
host string
apiKey string
bufferPool *sync.Pool
encoder encoder
contentEncoding ContentEncoding
retryOnStatus map[int]bool
disableRetry bool
maxRetries uint8
retryBackoff func(attempt uint8) time.Duration
}
type clientConfig struct {
contentEncoding ContentEncoding
encodingCompressionLevel EncodingCompressionLevel
retryOnStatus map[int]bool
disableRetry bool
maxRetries uint8
}
type internalRequest struct {
endpoint string
method string
contentType string
withRequest interface{}
withResponse interface{}
withQueryParams map[string]string
acceptedStatusCodes []int
functionName string
}
func newClient(cli *http.Client, host, apiKey string, cfg clientConfig) *client {
c := &client{
client: cli,
host: host,
apiKey: apiKey,
bufferPool: &sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
},
disableRetry: cfg.disableRetry,
maxRetries: cfg.maxRetries,
retryOnStatus: cfg.retryOnStatus,
}
if c.retryOnStatus == nil {
c.retryOnStatus = map[int]bool{
502: true,
503: true,
504: true,
}
}
if !c.disableRetry && c.retryBackoff == nil {
c.retryBackoff = func(attempt uint8) time.Duration {
return time.Second * time.Duration(attempt)
}
}
if !cfg.contentEncoding.IsZero() {
c.contentEncoding = cfg.contentEncoding
c.encoder = newEncoding(cfg.contentEncoding, cfg.encodingCompressionLevel)
}
return c
}
func (c *client) executeRequest(ctx context.Context, req *internalRequest) error {
internalError := &Error{
Endpoint: req.endpoint,
Method: req.method,
Function: req.functionName,
RequestToString: "empty request",
ResponseToString: "empty response",
MeilisearchApiError: meilisearchApiError{
Message: "empty meilisearch message",
},
StatusCodeExpected: req.acceptedStatusCodes,
encoder: c.encoder,
}
resp, err := c.sendRequest(ctx, req, internalError)
if err != nil {
return err
}
defer func() {
_ = resp.Body.Close()
}()
internalError.StatusCode = resp.StatusCode
b, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
err = c.handleStatusCode(req, resp.StatusCode, b, internalError)
if err != nil {
return err
}
err = c.handleResponse(req, b, internalError)
if err != nil {
return err
}
return nil
}
func (c *client) sendRequest(
ctx context.Context,
req *internalRequest,
internalError *Error,
) (*http.Response, error) {
apiURL, err := url.Parse(c.host + req.endpoint)
if err != nil {
return nil, fmt.Errorf("unable to parse url: %w", err)
}
if req.withQueryParams != nil {
query := apiURL.Query()
for key, value := range req.withQueryParams {
query.Set(key, value)
}
apiURL.RawQuery = query.Encode()
}
// Create request body
var body io.Reader = nil
if req.withRequest != nil {
if req.method == http.MethodGet || req.method == http.MethodHead {
return nil, ErrInvalidRequestMethod
}
if req.contentType == "" {
return nil, ErrRequestBodyWithoutContentType
}
rawRequest := req.withRequest
buf := c.bufferPool.Get().(*bytes.Buffer)
buf.Reset()
if b, ok := rawRequest.([]byte); ok {
buf.Write(b)
body = buf
} else if reader, ok := rawRequest.(io.Reader); ok {
// If the request body is an io.Reader then stream it directly
body = reader
} else {
// Otherwise convert it to JSON
var (
data []byte
err error
)
if marshaler, ok := rawRequest.(json.Marshaler); ok {
data, err = marshaler.MarshalJSON()
if err != nil {
return nil, internalError.WithErrCode(ErrCodeMarshalRequest,
fmt.Errorf("failed to marshal with MarshalJSON: %w", err))
}
if data == nil {
return nil, internalError.WithErrCode(ErrCodeMarshalRequest,
errors.New("MarshalJSON returned nil data"))
}
} else {
data, err = json.Marshal(rawRequest)
if err != nil {
return nil, internalError.WithErrCode(ErrCodeMarshalRequest,
fmt.Errorf("failed to marshal with json.Marshal: %w", err))
}
}
buf.Write(data)
body = buf
}
if !c.contentEncoding.IsZero() {
body, err = c.encoder.Encode(body)
if err != nil {
return nil, internalError.WithErrCode(ErrCodeMarshalRequest,
fmt.Errorf("failed to marshal with json.Marshal: %w", err))
}
}
}
// Create the HTTP request
request, err := http.NewRequestWithContext(ctx, req.method, apiURL.String(), body)
if err != nil {
return nil, fmt.Errorf("unable to create request: %w", err)
}
// adding request headers
if req.contentType != "" {
request.Header.Set("Content-Type", req.contentType)
}
if c.apiKey != "" {
request.Header.Set("Authorization", "Bearer "+c.apiKey)
}
if req.withResponse != nil && !c.contentEncoding.IsZero() {
request.Header.Set("Accept-Encoding", c.contentEncoding.String())
}
if req.withRequest != nil && !c.contentEncoding.IsZero() {
request.Header.Set("Content-Encoding", c.contentEncoding.String())
}
request.Header.Set("User-Agent", GetQualifiedVersion())
resp, err := c.do(request, internalError)
if err != nil {
return nil, err
}
if body != nil {
if buf, ok := body.(*bytes.Buffer); ok {
c.bufferPool.Put(buf)
}
}
return resp, nil
}
func (c *client) do(req *http.Request, internalError *Error) (resp *http.Response, err error) {
retriesCount := uint8(0)
for {
resp, err = c.client.Do(req)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return nil, internalError.WithErrCode(MeilisearchTimeoutError, err)
}
return nil, internalError.WithErrCode(MeilisearchCommunicationError, err)
}
// Exit if retries are disabled
if c.disableRetry {
break
}
// Check if response status is retryable and we haven't exceeded max retries
if c.retryOnStatus[resp.StatusCode] && retriesCount < c.maxRetries {
retriesCount++
// Close response body to prevent memory leaks
resp.Body.Close()
// Handle backoff with context cancellation support
backoff := c.retryBackoff(retriesCount)
timer := time.NewTimer(backoff)
select {
case <-req.Context().Done():
err := req.Context().Err()
timer.Stop()
return nil, internalError.WithErrCode(MeilisearchTimeoutError, err)
case <-timer.C:
// Retry after backoff
timer.Stop()
}
continue
}
break
}
// Return error if retries exceeded the maximum limit
if !c.disableRetry && retriesCount >= c.maxRetries {
return nil, internalError.WithErrCode(MeilisearchMaxRetriesExceeded, nil)
}
return resp, nil
}
func (c *client) handleStatusCode(req *internalRequest, statusCode int, body []byte, internalError *Error) error {
if req.acceptedStatusCodes != nil {
// A successful status code is required so check if the response status code is in the
// expected status code list.
for _, acceptedCode := range req.acceptedStatusCodes {
if statusCode == acceptedCode {
return nil
}
}
internalError.ErrorBody(body)
if internalError.MeilisearchApiError.Code == "" {
return internalError.WithErrCode(MeilisearchApiErrorWithoutMessage)
}
return internalError.WithErrCode(MeilisearchApiError)
}
return nil
}
func (c *client) handleResponse(req *internalRequest, body []byte, internalError *Error) (err error) {
if req.withResponse != nil {
if !c.contentEncoding.IsZero() {
if err := c.encoder.Decode(body, req.withResponse); err != nil {
return internalError.WithErrCode(ErrCodeResponseUnmarshalBody, err)
}
} else {
internalError.ResponseToString = string(body)
if internalError.ResponseToString == nullBody {
req.withResponse = nil
return nil
}
var err error
if resp, ok := req.withResponse.(json.Unmarshaler); ok {
err = resp.UnmarshalJSON(body)
req.withResponse = resp
} else {
err = json.Unmarshal(body, req.withResponse)
}
if err != nil {
return internalError.WithErrCode(ErrCodeResponseUnmarshalBody, err)
}
}
}
return nil
}