-
Notifications
You must be signed in to change notification settings - Fork 14
/
http.go
145 lines (127 loc) · 3.85 KB
/
http.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package config
import (
"net"
"net/http"
"net/url"
"time"
"github.com/anz-bank/sysl-go/validator"
)
func DefaultCommonDownstreamData() *CommonDownstreamData {
return &CommonDownstreamData{
ServiceURL: "",
ClientTransport: Transport{
Dialer: Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
},
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
ClientTimeout: 60 * time.Second,
}
}
// CommonDownstreamData collects all the client http configuration.
type CommonDownstreamData struct {
ServiceURL string `yaml:"serviceURL"`
ClientTransport Transport `yaml:"clientTransport"`
ClientTimeout time.Duration `yaml:"clientTimeout" validate:"timeout=1ms:60s"`
}
// Transport is used to initialise DefaultHTTPTransport.
type Transport struct {
Dialer Dialer `yaml:"dialer"`
MaxIdleConns int
IdleConnTimeout time.Duration
TLSHandshakeTimeout time.Duration
ExpectContinueTimeout time.Duration
ClientTLS *TLSConfig `yaml:"tls"`
ProxyURL string `yaml:"proxyURL"`
UseProxy bool `yaml:"useProxy"`
}
// Dialer is part of the Transport struct.
type Dialer struct {
Timeout time.Duration `yaml:"timeout"`
KeepAlive time.Duration `yaml:"keepAlive"`
DualStack bool `yaml:"dualStack"`
}
func (g *CommonDownstreamData) Validate() error {
if err := validator.Validate(g); err != nil {
return err
}
if g.ClientTransport.ClientTLS != nil {
if err := g.ClientTransport.ClientTLS.Validate(); err != nil {
return err
}
}
return nil
}
type CommonServerConfig struct {
HostName string `yaml:"hostName"`
Port int `yaml:"port" validate:"min=0,max=65534"`
TLS *TLSConfig `yaml:"tls"`
}
type CommonHTTPServerConfig struct {
Common CommonServerConfig `yaml:"common"`
BasePath string `yaml:"basePath" validate:"startswith=/"`
ReadTimeout time.Duration `yaml:"readTimeout" validate:"nonnil"`
WriteTimeout time.Duration `yaml:"writeTimeout" validate:"nonnil"`
}
func (c *CommonHTTPServerConfig) Validate() error {
// existing validation
if err := validator.Validate(c); err != nil {
return err
}
return nil
}
func proxyHandlerFromConfig(cfg *Transport) func(req *http.Request) (*url.URL, error) {
if cfg.UseProxy {
if len(cfg.ProxyURL) > 0 {
return func(req *http.Request) (*url.URL, error) {
proxyURL, err := url.Parse(cfg.ProxyURL)
if err != nil {
return http.ProxyFromEnvironment(req)
}
return proxyURL, err
}
}
return http.ProxyFromEnvironment
}
return nil
}
// defaultHTTPTransport returns a new *http.Transport with the same configuration as http.DefaultTransport.
func defaultHTTPTransport(cfg *Transport) (*http.Transport, error) {
// Finalise the handler loading
tlsConfig, err := MakeTLSConfig(cfg.ClientTLS)
if err != nil {
return nil, err
}
return &http.Transport{
Proxy: proxyHandlerFromConfig(cfg),
DialContext: (&net.Dialer{
Timeout: cfg.Dialer.Timeout,
KeepAlive: cfg.Dialer.KeepAlive,
DualStack: cfg.Dialer.DualStack,
}).DialContext,
MaxIdleConns: cfg.MaxIdleConns,
IdleConnTimeout: cfg.IdleConnTimeout,
TLSHandshakeTimeout: cfg.TLSHandshakeTimeout,
ExpectContinueTimeout: cfg.ExpectContinueTimeout,
TLSClientConfig: tlsConfig,
}, nil
}
// DefaultHTTPClient returns a new *http.Client with sensible defaults, in particular it has a timeout set.
func DefaultHTTPClient(cfg *CommonDownstreamData) (*http.Client, error) {
if cfg == nil {
cfg = DefaultCommonDownstreamData()
}
transport, err := defaultHTTPTransport(&cfg.ClientTransport)
if err != nil {
return nil, err
}
return &http.Client{
Timeout: cfg.ClientTimeout,
Transport: transport,
}, nil
}