-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
300 lines (262 loc) · 7.34 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
package retryhttp
import (
"bytes"
"encoding/json"
"io"
"net"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/avast/retry-go/v3"
"github.com/hashicorp/go-cleanhttp"
"github.com/pkg/errors"
)
// LenReader is an interface implemented by many in-memory io.Reader's. Used
// for automatically sending the right Content-Length header when possible.
type LenReader interface {
Len() int
}
type Config struct {
// Attempts must be greater than 0, otherwise it will be set to 1
Attempts uint
MaxDelay time.Duration
Timeout time.Duration
Delay time.Duration
MaxJitter time.Duration
}
type Properties struct {
Config
OnRetry retry.OnRetryFunc
DelayType retry.DelayTypeFunc
RetryLogicFunc RetryLogicFunc
RetryHTTPCodes []int
JSONUnmarshal func(data []byte, v interface{}) error
}
// RetryLogicFunc is a function that returns error if request should be retried
// disabling check for RetryHTTPCodes
type RetryLogicFunc func(resp *http.Response) error
var (
defaultClient *Client
mu sync.Mutex
defaultOnce sync.Once
defaultConfig = Config{
Attempts: 3,
MaxDelay: time.Second * 2,
Timeout: time.Second,
Delay: time.Millisecond * 100,
MaxJitter: time.Millisecond * 10,
}
ErrReceiverNotSet = errors.New("receiver not set")
)
type Client struct {
client *http.Client
retryOptions []retry.Option
retryLogicFunc RetryLogicFunc
jsonUnmarshal func(data []byte, v interface{}) error
}
func SetDefaultConfig(config Config) {
mu.Lock()
defer mu.Unlock()
if defaultClient != nil {
panic("can not retry_http.SetDefaultConfig after call retry_http.Default()")
}
defaultConfig = config
}
func GetDefaultConfig() Config {
mu.Lock()
defer mu.Unlock()
return defaultConfig
}
func Default() *Client {
defaultOnce.Do(func() {
defaultClient = New()
})
return defaultClient
}
// Get execute http method and return result into *http.Response
func Get(url string) (*http.Response, error) {
return Default().Get(url)
}
// Post execute http method and return result into *http.Response
func Post(url, contentType string, body io.Reader) (resp *http.Response, err error) {
return Default().Post(url, contentType, body)
}
// PostForm execute http method and return result into *http.Response
func PostForm(url string, data url.Values) (resp *http.Response, err error) {
return Default().PostForm(url, data)
}
// Head execute http method and return result into *http.Response
func Head(url string) (resp *http.Response, err error) {
return Default().Head(url)
}
// Do execute http method and return result into *http.Response
func Do(request *http.Request) (*http.Response, error) {
return Default().Do(request)
}
// Do2Bytes execute http method and return result into []byte
func Do2Bytes(request *http.Request) ([]byte, error) {
return Default().Do2Bytes(request)
}
// Do2JSON execute http method and save json response into receiver struct
func Do2JSON(request *http.Request, receiver interface{}) error {
return Default().Do2JSON(request, receiver)
}
func New(customize ...func(properties *Properties)) *Client {
prop := &Properties{
Config: defaultConfig,
DelayType: retry.CombineDelay(retry.BackOffDelay, retry.RandomDelay),
OnRetry: func(n uint, err error) {},
JSONUnmarshal: json.Unmarshal,
}
for _, c := range customize {
c(prop)
}
if prop.RetryLogicFunc == nil {
prop.RetryLogicFunc = func(resp *http.Response) error {
for _, httpCode := range prop.RetryHTTPCodes {
if resp.StatusCode == httpCode {
return errors.Errorf("got code %d", resp.StatusCode)
}
}
return nil
}
}
if prop.Attempts < 1 {
prop.Attempts = 1
}
opts := []retry.Option{
retry.MaxDelay(prop.MaxDelay),
retry.Attempts(prop.Attempts),
retry.OnRetry(prop.OnRetry),
retry.Delay(prop.Delay),
retry.MaxJitter(prop.MaxJitter),
retry.DelayType(prop.DelayType),
retry.LastErrorOnly(true),
}
client := cleanhttp.DefaultPooledClient()
client.Timeout = prop.Timeout
return &Client{
client: client,
retryOptions: opts,
retryLogicFunc: prop.RetryLogicFunc,
jsonUnmarshal: prop.JSONUnmarshal,
}
}
// Get execute http method and return result into *http.Response
func (c *Client) Get(url string) (resp *http.Response, err error) {
request, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
return c.doWithRetries(request)
}
// Post execute http method and return result into *http.Response
func (c *Client) Post(url, contentType string, body io.Reader) (resp *http.Response, err error) {
request, err := http.NewRequest("POST", url, body)
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", contentType)
return c.doWithRetries(request)
}
// PostForm execute http method and return result into *http.Response
func (c *Client) PostForm(url string, data url.Values) (resp *http.Response, err error) {
return c.Post(url, "application/x-www-form-urlencoded", strings.NewReader(data.Encode()))
}
// Head execute http method and return result into *http.Response
func (c *Client) Head(url string) (resp *http.Response, err error) {
request, err := http.NewRequest("HEAD", url, nil)
if err != nil {
return nil, err
}
return c.doWithRetries(request)
}
// Do execute http method and return result into *http.Response
func (c *Client) Do(request *http.Request) (*http.Response, error) {
return c.doWithRetries(request)
}
// Do2Bytes execute http method and return result into []byte
func (c *Client) Do2Bytes(request *http.Request) ([]byte, error) {
response, err := c.doWithRetries(request)
defer func() {
if response != nil {
_ = response.Body.Close()
}
}()
if err != nil {
return nil, err
}
result, err := io.ReadAll(response.Body)
if err != nil {
return nil, errors.Wrap(err, "io.ReadAll")
}
return result, nil
}
// Do2JSON execute http method and save json response into receiver struct
func (c *Client) Do2JSON(request *http.Request, receiver interface{}) error {
b, err := c.Do2Bytes(request)
if err != nil {
return err
}
if receiver == nil {
return ErrReceiverNotSet
}
err = c.jsonUnmarshal(b, receiver)
if err != nil {
return errors.Wrap(err, "json.Unmarshal")
}
return nil
}
func (c *Client) doWithRetries(request *http.Request) (resp *http.Response, err error) {
var (
bodyBuffer []byte
contentLength int64
)
if request.Body != nil {
bodyBuffer, err = io.ReadAll(request.Body)
if err != nil {
return nil, err
}
contentLength = int64(len(bodyBuffer))
request.Body = io.NopCloser(bytes.NewReader(bodyBuffer))
}
action := func() error {
if bodyBuffer != nil {
request.Body = io.NopCloser(bytes.NewReader(bodyBuffer))
request.ContentLength = contentLength
request.GetBody = func() (io.ReadCloser, error) {
return io.NopCloser(bytes.NewReader(bodyBuffer)), nil
}
}
//nolint:bodyclose
resp, err = c.client.Do(request)
if err != nil {
if isRetryableError(err) {
return err
}
return retry.Unrecoverable(err)
}
if resp != nil {
return c.retryLogicFunc(resp)
}
return nil
}
opts := make([]retry.Option, 0, len(c.retryOptions)+1)
opts = append(opts, retry.Context(request.Context()))
opts = append(opts, c.retryOptions...)
err = retry.Do(action, opts...)
return resp, err
}
func isRetryableError(err error) bool {
if err == nil {
return false
}
var errNet net.Error
ok := errors.As(err, &errNet)
if ok && errNet.Timeout() {
return true
}
return false
}