-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
71 lines (55 loc) · 1.3 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
package main
import (
"context"
"net"
"net/http"
"strings"
"time"
"golang.org/x/time/rate"
)
type MdClient struct {
Client *http.Client
RateLimiterMap map[string]*rate.Limiter
// Mutex sync.Mutex
}
func NewMdClient() *MdClient {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 60 * time.Second,
}).DialContext,
MaxIdleConns: 100,
MaxConnsPerHost: 100,
MaxIdleConnsPerHost: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
client := &http.Client{
Transport: transport,
}
rlMap := map[string]*rate.Limiter{
"/at-home/server/": rate.NewLimiter(rate.Every(time.Minute/38), 1),
}
return &MdClient{
Client: client,
RateLimiterMap: rlMap,
}
}
func (c *MdClient) Get(url string) (*http.Response, error) {
if strings.Contains(url, "/at-home/server/") {
limiter := c.RateLimiterMap["/at-home/server/"]
err := limiter.Wait(context.Background())
if err != nil {
return nil, err
}
}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept-Encoding", "identity")
res, err := c.Client.Do(req)
return res, err
}