-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
76 lines (68 loc) · 2.05 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
package client
import (
"fmt"
"github.com/bocchi-the-cache/inspector/pkg/common/logger"
"github.com/bocchi-the-cache/inspector/pkg/monitor"
"io/ioutil"
"net"
"net/http"
"time"
"github.com/spf13/viper"
)
var BaselineFetcher *Fetcher
var TestFetcher *Fetcher
func Init() {
BaselineFetcher = NewHttpFetcher(viper.GetString("host.baseline"))
TestFetcher = NewHttpFetcher(viper.GetString("host.test"))
}
type Fetcher struct {
HttpClient *http.Client
RewriteHost string
}
func NewHttpFetcher(rHost string) *Fetcher {
c := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 500,
MaxIdleConnsPerHost: 500,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
Timeout: time.Duration(15) * time.Second,
}
f := &Fetcher{
HttpClient: c,
RewriteHost: rHost,
}
return f
}
func (f *Fetcher) Do(r *http.Request) (int, http.Header, []byte, error) {
// TODO: Host rewrite
// For client requests, the URL's Host specifies the server to
// connect to, while the Request's Host field optionally
req, err := http.NewRequest(r.Method, fmt.Sprintf("http://%s%s", r.Host, r.URL.String()), r.Body)
if err != nil {
monitor.RequestSendTotalCounterIncr(r.Method, r.Host, f.RewriteHost, "ErrorRequest")
logger.Error("new request error, err: %s", err)
}
req.Header = r.Header
req.URL.Host = f.RewriteHost
resp, err := f.HttpClient.Do(req)
if err != nil {
monitor.RequestSendTotalCounterIncr(r.Method, r.Host, f.RewriteHost, "ErrorSend")
return 0, nil, nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
monitor.RequestSendTotalCounterIncr(r.Method, r.Host, f.RewriteHost, "ErrorReadBody")
return resp.StatusCode, nil, nil, err
}
monitor.RequestSendTotalCounterIncr(r.Method, r.Host, f.RewriteHost, resp.Status)
return resp.StatusCode, resp.Header, body, nil
}