/
client.go
94 lines (82 loc) · 2.6 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
package rehttp
import (
"crypto/tls"
"net"
"net/http"
"github.com/facebookgo/httpcontrol"
"github.com/linkosmos/redial"
)
// -
const (
RequestProto = "HTTP/1.1"
RequestProtoMinor = 0
RequestProtoMajor = 1
)
// Client - http.: Transport, Client wrapper
type Client struct {
RequestProto string
RequestProtoMinor, RequestProtoMajor int
// A Config structure is used to configure a TLS client or server.
// After one has been passed to a TLS function it must not be
// modified. A Config may be reused; the tls package will also not
// modify it.
TLS *tls.Config
// Transport is an implementation of RoundTripper that supports HTTP,
// HTTPS, and HTTP proxies (for either HTTP or HTTPS with CONNECT).
// Transport can also cache connections for future re-use.
Transport *httpcontrol.Transport
// A Client is an HTTP client. Its zero value (DefaultClient) is a
// usable client that uses DefaultTransport.
//
// The Client's Transport typically has internal state (cached TCP
// connections), so Clients should be reused instead of created as
// needed. Clients are safe for concurrent use by multiple goroutines.
//
// A Client is higher-level than a RoundTripper (such as Transport)
// and additionally handles HTTP details such as cookies and
// redirects.
Client ClientRequester
// rehttp.Client options
ClientOptions *Options
}
// New - returns Request Client
func New(op *Options, address, port string) (r *Client, err error) {
if op == nil {
op = NewOptions()
}
dialer, err := redial.New(net.Dialer{
Timeout: op.DialerTimeout,
Deadline: op.DialerDeadline,
KeepAlive: op.DialerKeepAlive,
DualStack: op.DialerDualStack,
}, address, port, op.ConnPoolInitial, op.ConnPoolMax)
if err != nil {
return nil, err
}
r = &Client{
RequestProto: RequestProto,
RequestProtoMinor: RequestProtoMinor,
RequestProtoMajor: RequestProtoMajor,
TLS: &tls.Config{
InsecureSkipVerify: op.TLSInsecureSkipVerify,
},
ClientOptions: op, // Setting rehttp.Client options
}
// Setting up TRANSPORT
r.Transport = &httpcontrol.Transport{
Dial: dialer.Dial,
TLSClientConfig: r.TLS,
MaxTries: op.TransportMaxTries,
DisableKeepAlives: op.TransportDisableKeepAlives,
DisableCompression: op.TransportDisableCompression,
MaxIdleConnsPerHost: op.TransportMaxIdleConnsPerHost,
RequestTimeout: op.TransportRequestTimeout,
ResponseHeaderTimeout: op.TransportResponseHeaderTimeout,
}
// Setting up CLIENT, higher level API of TRANSPORT
r.Client = &http.Client{
Transport: r.Transport,
Timeout: op.ClientTimeout,
}
return r, nil
}