-
Notifications
You must be signed in to change notification settings - Fork 8
/
http_client.go
109 lines (95 loc) · 2.96 KB
/
http_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
package httputilmore
import (
"bytes"
"fmt"
"log"
"net"
"net/http"
"time"
//"github.com/rs/zerolog/log"
)
var (
DIAL_TIMEOUT int64 = 5
TLS_TIMEOUT int = 5
HTTP_TIMEOUT int = 10
)
// The default Go HTTP client never times out.
// This HTTP client provides default and updatable timeouts
// More at: https://medium.com/@nate510/don-t-use-go-s-default-http-client-4804cb19f779#.ymd655pgz
func NewHttpClient() *http.Client {
dial_timeout, _ := time.ParseDuration(fmt.Sprintf("%vs", DIAL_TIMEOUT))
netTransport := &http.Transport{
Dial: (&net.Dialer{
Timeout: dial_timeout}).Dial,
TLSHandshakeTimeout: 5 * time.Second}
netClient := &http.Client{
Timeout: 10 * time.Second,
Transport: netTransport}
return netClient
}
func GetRequestRateLimited(client *http.Client, reqURL string, useXrlHyphen bool, fn FnLogRateLimitInfo) (*http.Response, error) {
req, err := http.NewRequest("GET", reqURL, nil)
if err != nil {
return &http.Response{}, err
}
return DoRequestRateLimited(client, req, useXrlHyphen, fn)
}
// DoRequestRateLimited will pause a request for the time specified in the
// HTTP response headers.
func DoRequestRateLimited(client *http.Client, req *http.Request, useXrlHyphen bool, fnLog FnLogRateLimitInfo) (*http.Response, error) {
resp, err := client.Do(req)
if err != nil {
return resp, err
}
rlstat := NewResponseRateLimitInfo(resp, useXrlHyphen)
if rlstat.XRateLimitRemaining <= 0 {
fnLog(rlstat)
time.Sleep(time.Duration(rlstat.XRateLimitWindow) * time.Second)
return resp, nil
} else if rlstat.StatusCode == 429 {
fnLog(rlstat)
if rlstat.RetryAfter > 0 {
time.Sleep(time.Duration(rlstat.RetryAfter) * time.Second)
} else if rlstat.XRateLimitWindow > 0 {
time.Sleep(time.Duration(rlstat.XRateLimitWindow) * time.Second)
} else {
time.Sleep(time.Duration(60) * time.Second)
}
return client.Do(req)
}
return resp, nil
}
func LogRequestRateLimited(rlstat RateLimitInfo) {
logInfo:= map[string]interface{}{
"action": "http_rate_limited",
"status_code": rlstat.StatusCode,
"retry-after": rlstat.RetryAfter,
"x-rate-limit-remaining": rlstat.XRateLimitRemaining,
"x-rate-limit-window": rlstat.XRateLimitWindow,
"message":"Request has been rated limited."}
log.Printf("%v\n" , logInfo)
/*
log.Info().
Str("action", "http_rate_limited").
Int("status_code", rlstat.StatusCode).
Int("retry-after", rlstat.RetryAfter).
Int("x-rate-limit-remaining", rlstat.XRateLimitRemaining).
Int("x-rate-limit-window", rlstat.XRateLimitWindow).
Msg("Request has been rated limited.")
*/
}
type ClientMore struct {
Client *http.Client
}
func (cm *ClientMore) PostToJSON(postURL string, body interface{}) (*http.Response, error) {
jsonBytes, err := json.Marshal(body)
if err != nil {
return &http.Response{}, err
}
req, err := http.NewRequest("POST", postURL, bytes.NewBuffer(jsonBytes))
if err != nil {
return &http.Response{}, err
}
req.Header.Set(HeaderContentType, ContentTypeAppJsonUtf8)
return cm.Client.Do(req)
}