/
transport.go
206 lines (166 loc) · 5.62 KB
/
transport.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
200
201
202
203
204
205
206
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package config
import (
"fmt"
"time"
)
const (
// DefaultDialKeepAlive is the default amount of time to keep alive
// connections.
DefaultDialKeepAlive = 30 * time.Second
// DefaultDialTimeout is the amount of time to attempt to dial before timing
// out.
DefaultDialTimeout = 30 * time.Second
// DefaultIdleConnTimeout is the default connection timeout for idle
// connections.
DefaultIdleConnTimeout = 5 * time.Second
// DefaultMaxIdleConns is the default number of maximum idle connections.
DefaultMaxIdleConns = 0
// DefaultMaxIdleConnsPerHost is the default number of maximum idle
// connections per host.
DefaultMaxIdleConnsPerHost = 100
// DefaultTLSHandshakeTimeout is the amount of time to negotiate the TLS
// handshake.
DefaultTLSHandshakeTimeout = 10 * time.Second
)
// TransportConfig is the configuration to tune low-level APIs for the
// interactions on the wire.
type TransportConfig struct {
// DialKeepAlive is the amount of time for keep-alives.
DialKeepAlive *time.Duration `mapstructure:"dial_keep_alive"`
// DialTimeout is the amount of time to wait to establish a connection.
DialTimeout *time.Duration `mapstructure:"dial_timeout"`
// DisableKeepAlives determines if keep-alives should be used. Disabling this
// significantly decreases performance.
DisableKeepAlives *bool `mapstructure:"disable_keep_alives"`
// IdleConnTimeout is the timeout for idle connections.
IdleConnTimeout *time.Duration `mapstructure:"idle_conn_timeout"`
// MaxIdleConns is the maximum number of total idle connections.
MaxIdleConns *int `mapstructure:"max_idle_conns"`
// MaxIdleConnsPerHost is the maximum number of idle connections per remote
// host.
//
// The majority of connections are established with one host, the Consul agent.
// To achieve the shortest latency between a Consul service update to a task
// execution, configure the max_idle_conns_per_host to a number proportional to
// the number of services in automation across all tasks.
//
// This value must be lower than the configured http_max_conns_per_client
// for the Consul agent. Note that requests made by Terraform subprocesses
// or any other process on the same host as Consul-Terraform-Sync will
// contribute to the Consul agent http_max_conns_per_client.
//
// If max_idle_conns_per_host or the number of services in automation is greater
// than the Consul agent limit, Consul-Terraform-Sync may error due to
// connection limits (429).
MaxIdleConnsPerHost *int `mapstructure:"max_idle_conns_per_host"`
// TLSHandshakeTimeout is the amount of time to wait to complete the TLS
// handshake.
TLSHandshakeTimeout *time.Duration `mapstructure:"tls_handshake_timeout"`
}
// DefaultTransportConfig returns a configuration that is populated with the
// default values.
func DefaultTransportConfig() *TransportConfig {
return &TransportConfig{}
}
// Copy returns a deep copy of this configuration.
func (c *TransportConfig) Copy() *TransportConfig {
if c == nil {
return nil
}
var o TransportConfig
o.DialKeepAlive = c.DialKeepAlive
o.DialTimeout = c.DialTimeout
o.DisableKeepAlives = c.DisableKeepAlives
o.IdleConnTimeout = c.IdleConnTimeout
o.MaxIdleConns = c.MaxIdleConns
o.MaxIdleConnsPerHost = c.MaxIdleConnsPerHost
o.TLSHandshakeTimeout = c.TLSHandshakeTimeout
return &o
}
// Merge combines all values in this configuration with the values in the other
// configuration, with values in the other configuration taking precedence.
// Maps and slices are merged, most other values are overwritten. Complex
// structs define their own merge functionality.
func (c *TransportConfig) Merge(o *TransportConfig) *TransportConfig {
if c == nil {
if o == nil {
return nil
}
return o.Copy()
}
if o == nil {
return c.Copy()
}
r := c.Copy()
if o.DialKeepAlive != nil {
r.DialKeepAlive = o.DialKeepAlive
}
if o.DialTimeout != nil {
r.DialTimeout = o.DialTimeout
}
if o.DisableKeepAlives != nil {
r.DisableKeepAlives = o.DisableKeepAlives
}
if o.IdleConnTimeout != nil {
r.IdleConnTimeout = o.IdleConnTimeout
}
if o.MaxIdleConns != nil {
r.MaxIdleConns = o.MaxIdleConns
}
if o.MaxIdleConnsPerHost != nil {
r.MaxIdleConnsPerHost = o.MaxIdleConnsPerHost
}
if o.TLSHandshakeTimeout != nil {
r.TLSHandshakeTimeout = o.TLSHandshakeTimeout
}
return r
}
// Finalize ensures there no nil pointers.
func (c *TransportConfig) Finalize() {
if c.DialKeepAlive == nil {
c.DialKeepAlive = TimeDuration(DefaultDialKeepAlive)
}
if c.DialTimeout == nil {
c.DialTimeout = TimeDuration(DefaultDialTimeout)
}
if c.DisableKeepAlives == nil {
c.DisableKeepAlives = Bool(false)
}
if c.IdleConnTimeout == nil {
c.IdleConnTimeout = TimeDuration(DefaultIdleConnTimeout)
}
if c.MaxIdleConns == nil {
c.MaxIdleConns = Int(DefaultMaxIdleConns)
}
if c.MaxIdleConnsPerHost == nil {
c.MaxIdleConnsPerHost = Int(DefaultMaxIdleConnsPerHost)
}
if c.TLSHandshakeTimeout == nil {
c.TLSHandshakeTimeout = TimeDuration(DefaultTLSHandshakeTimeout)
}
}
// GoString defines the printable version of this struct.
func (c *TransportConfig) GoString() string {
if c == nil {
return "(*TransportConfig)(nil)"
}
return fmt.Sprintf("&TransportConfig{"+
"DialKeepAlive:%s, "+
"DialTimeout:%s, "+
"DisableKeepAlives:%t, "+
"IdleConnTimeout:%d, "+
"MaxIdleConns:%d, "+
"MaxIdleConnsPerHost:%d, "+
"TLSHandshakeTimeout:%s"+
"}",
TimeDurationVal(c.DialKeepAlive),
TimeDurationVal(c.DialTimeout),
BoolVal(c.DisableKeepAlives),
TimeDurationVal(c.IdleConnTimeout),
IntVal(c.MaxIdleConns),
IntVal(c.MaxIdleConnsPerHost),
TimeDurationVal(c.TLSHandshakeTimeout),
)
}