-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
81 lines (70 loc) · 1.67 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
package main
import (
"crypto/tls"
"fmt"
"math/rand"
"net"
"net/http"
"net/http/cookiejar"
"time"
)
type Stop struct {
error
}
func NewTimeoutClient() (*http.Client, error) {
jar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
return &http.Client{
Jar: jar,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Dial: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).Dial,
TLSHandshakeTimeout: 30 * time.Second,
ResponseHeaderTimeout: 30 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}, nil
}
func Retry(attempts int, sleep time.Duration, f func() (*http.Response, error)) (*http.Response, error) {
resp, err := f()
if err != nil {
if s, ok := err.(*Stop); ok {
// Return the original error for later checking
return nil, s.error
}
if attempts--; attempts > 0 {
// Add some randomness to prevent creating a Thundering Herd
jitter := time.Duration(rand.Int63n(int64(sleep)))
sleep = sleep + jitter/2
time.Sleep(sleep)
return Retry(attempts, 2*sleep, f)
}
return nil, err
}
return resp, nil
}
func RetryRequest(client *http.Client, request *http.Request, attempts int, sleep time.Duration) (*http.Response, error) {
return Retry(attempts, sleep, func() (*http.Response, error) {
resp, err := client.Do(request)
if err != nil {
return nil, err
}
s := resp.StatusCode
switch {
case s >= 500:
// Retry
return nil, fmt.Errorf("server error: %v", s)
case s >= 400:
// Don't retry, it was client's fault
return nil, Stop{fmt.Errorf("client error: %v", s)}
default:
// Happy
return resp, nil
}
})
}