-
Notifications
You must be signed in to change notification settings - Fork 25
/
client.go
199 lines (171 loc) · 6.78 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package http
import (
"context"
"net"
"net/http"
"go.uber.org/fx"
"github.com/fluxninja/aperture/v2/pkg/config"
"github.com/fluxninja/aperture/v2/pkg/log"
"github.com/fluxninja/aperture/v2/pkg/net/tlsconfig"
)
// ClientModule is an fx module that provides annotated HTTP client.
func ClientModule() fx.Option {
return fx.Options(
ClientConstructor{}.Annotate(),
)
}
// ClientConstructor holds fields to create an annotated instance of HTTP client.
type ClientConstructor struct {
Name string
ConfigKey string
DefaultConfig HTTPClientConfig
}
// HTTPClientConfig holds configuration for HTTP Client.
// swagger:model
// +kubebuilder:object:generate=true
type HTTPClientConfig struct {
// Network level keep-alive duration
NetworkKeepAlive config.Duration `json:"network_keep_alive" validate:"gte=0s" default:"30s"`
// Timeout for making network connection
NetworkTimeout config.Duration `json:"network_timeout" validate:"gte=0s" default:"30s"`
// HTTP client timeout - Timeouts include connection time, redirects, reading the response and so on. 0 = no timeout.
Timeout config.Duration `json:"timeout" validate:"gte=0s" default:"60s"`
// Proxy Connect Header - `map[string][]string`
ProxyConnectHeader http.Header `json:"proxy_connect_header,omitempty" validate:"omitempty"`
// TLS Handshake Timeout. 0 = no timeout
TLSHandshakeTimeout config.Duration `json:"tls_handshake_timeout" validate:"gte=0s" default:"10s"`
// Expect Continue Timeout. 0 = no timeout.
ExpectContinueTimeout config.Duration `json:"expect_continue_timeout" validate:"gte=0s" default:"1s"`
// Response Header Timeout. 0 = no timeout.
ResponseHeaderTimeout config.Duration `json:"response_header_timeout" validate:"gte=0s" default:"0s"`
// Idle Connection Timeout. 0 = no timeout.
IdleConnTimeout config.Duration `json:"idle_connection_timeout" validate:"gte=0s" default:"90s"`
// SSL/TLS key log file (useful for debugging)
KeyLogWriter string `json:"key_log_file"`
// Client TLS configuration
ClientTLSConfig tlsconfig.ClientTLSConfig `json:"tls"`
// Max Idle Connections per host. 0 = no limit.
MaxIdleConnsPerHost int `json:"max_idle_connections_per_host" validate:"gte=0" default:"5"`
// Max Idle Connections. 0 = no limit.
MaxIdleConns int `json:"max_idle_connections" validate:"gte=0" default:"100"`
// Max Connections Per Host. 0 = no limit.
MaxConnsPerHost int `json:"max_conns_per_host" validate:"gte=0" default:"0"`
// Max Response Header Bytes. 0 = no limit.
MaxResponseHeaderBytes int64 `json:"max_response_header_bytes" validate:"gte=0" default:"0"`
// Write Buffer Size. 0 = 4 KB.
WriteBufferSize int `json:"write_buffer_size" validate:"gte=0" default:"0"`
// Read Buffer Size. 0 = 4 KB
ReadBufferSize int `json:"read_buffer_size" validate:"gte=0" default:"0"`
// Disable Compression
DisableCompression bool `json:"disable_compression" default:"false"`
// Use Proxy
UseProxy bool `json:"use_proxy" default:"false"`
// Disable HTTP Keepalive
DisableKeepAlives bool `json:"disable_keep_alives" default:"false"`
}
// Annotate creates an annotated instance of HTTP Client.
func (constructor ClientConstructor) Annotate() fx.Option {
if constructor.ConfigKey == "" {
log.Panic().Msg("config key not provided")
}
name := config.NameTag(constructor.Name)
cfgName := config.NameTag(constructor.Name + "-config")
return fx.Provide(
fx.Annotate(
constructor.provideHTTPClient,
fx.ResultTags(name, name, cfgName),
),
)
}
func (constructor ClientConstructor) provideHTTPClient(unmarshaller config.Unmarshaller, lifecycle fx.Lifecycle) (*http.Client, *MiddlewareChain, *HTTPClientConfig, error) {
var err error
config := constructor.DefaultConfig
if err = unmarshaller.UnmarshalKey(constructor.ConfigKey, &config); err != nil {
log.Error().Err(err).Msg("Unable to deserialize httpclient configuration!")
return nil, nil, nil, err
}
client, err := ClientFromConfig(config)
if err != nil {
return nil, nil, nil, err
}
// return a middleware chain -- call invokes on this object to chain middleware functions
mwc := &MiddlewareChain{
client: client,
middlewares: []Middleware{},
}
lifecycle.Append(fx.Hook{
OnStart: func(context.Context) error {
// build middleware chain
mwc.buildChain()
return nil
},
OnStop: func(context.Context) error {
return nil
},
})
return client, mwc, &config, nil
}
// ClientFromConfig creates http client from already parsed config.
func ClientFromConfig(config HTTPClientConfig) (*http.Client, error) {
tlsConfig, err := config.ClientTLSConfig.GetTLSConfig()
if err != nil {
return nil, err
}
transport := &http.Transport{
TLSClientConfig: tlsConfig,
DialContext: (&net.Dialer{
Timeout: config.NetworkTimeout.AsDuration(),
KeepAlive: config.NetworkKeepAlive.AsDuration(),
}).DialContext,
TLSHandshakeTimeout: config.TLSHandshakeTimeout.AsDuration(),
DisableKeepAlives: config.DisableKeepAlives,
DisableCompression: config.DisableCompression,
MaxIdleConns: config.MaxIdleConns,
MaxIdleConnsPerHost: config.MaxIdleConnsPerHost,
MaxConnsPerHost: config.MaxConnsPerHost,
IdleConnTimeout: config.IdleConnTimeout.AsDuration(),
ResponseHeaderTimeout: config.ResponseHeaderTimeout.AsDuration(),
ExpectContinueTimeout: config.ExpectContinueTimeout.AsDuration(),
ProxyConnectHeader: config.ProxyConnectHeader,
MaxResponseHeaderBytes: config.MaxResponseHeaderBytes,
WriteBufferSize: config.WriteBufferSize,
ReadBufferSize: config.ReadBufferSize,
}
if config.UseProxy {
transport.Proxy = http.ProxyFromEnvironment
}
client := &http.Client{
Transport: transport,
Timeout: config.Timeout.AsDuration(),
}
return client, nil
}
// inspired by https://github.com/improbable-eng/go-httpwares/blob/master/tripperware.go
// RoundTripperFunc wraps a func to make it into a http.RoundTripper. Similar to http.HandleFunc.
type RoundTripperFunc func(*http.Request) (*http.Response, error)
// RoundTrip implements http.RoundTripper.
func (rtf RoundTripperFunc) RoundTrip(request *http.Request) (*http.Response, error) {
return rtf(request)
}
// Middleware is signature of all http middleware.
type Middleware func(next http.RoundTripper) http.RoundTripper
// MiddlewareChain holds a chain of middleware.
type MiddlewareChain struct {
client *http.Client
middlewares []Middleware
}
// Chain appends provided middleware to the MiddlewareChain.
// Middleware will be chained based on the order of Invokes.
func (mwc *MiddlewareChain) Chain(middlewares ...Middleware) {
mwc.middlewares = append(mwc.middlewares, middlewares...)
}
func (mwc *MiddlewareChain) buildChain() {
if len(mwc.middlewares) == 0 {
return
}
transport := mwc.client.Transport
for i := len(mwc.middlewares) - 1; i >= 0; i-- {
transport = mwc.middlewares[i](transport)
}
mwc.client.Transport = transport
}