@@ -47,35 +47,34 @@ type tlsCacheKey struct {
47
47
keyData string
48
48
certFile string
49
49
keyFile string
50
- getCert string
51
50
serverName string
52
51
nextProtos string
53
- dial string
54
52
disableCompression bool
55
- proxy string
56
53
}
57
54
58
55
func (t tlsCacheKey ) String () string {
59
56
keyText := "<none>"
60
57
if len (t .keyData ) > 0 {
61
58
keyText = "<redacted>"
62
59
}
63
- return fmt .Sprintf ("insecure:%v, caData:%#v, certData:%#v, keyData:%s, getCert: %s, serverName:%s, dial:%s disableCompression:%t, proxy: %s " , t .insecure , t .caData , t .certData , keyText , t .getCert , t . serverName , t .dial , t . disableCompression , t . proxy )
60
+ return fmt .Sprintf ("insecure:%v, caData:%#v, certData:%#v, keyData:%s, serverName:%s, disableCompression:%t" , t .insecure , t .caData , t .certData , keyText , t .serverName , t .disableCompression )
64
61
}
65
62
66
63
func (c * tlsTransportCache ) get (config * Config ) (http.RoundTripper , error ) {
67
- key , err := tlsConfigKey (config )
64
+ key , canCache , err := tlsConfigKey (config )
68
65
if err != nil {
69
66
return nil , err
70
67
}
71
68
72
- // Ensure we only create a single transport for the given TLS options
73
- c .mu .Lock ()
74
- defer c .mu .Unlock ()
69
+ if canCache {
70
+ // Ensure we only create a single transport for the given TLS options
71
+ c .mu .Lock ()
72
+ defer c .mu .Unlock ()
75
73
76
- // See if we already have a custom transport for this config
77
- if t , ok := c .transports [key ]; ok {
78
- return t , nil
74
+ // See if we already have a custom transport for this config
75
+ if t , ok := c .transports [key ]; ok {
76
+ return t , nil
77
+ }
79
78
}
80
79
81
80
// Get the TLS options for this client config
@@ -110,33 +109,41 @@ func (c *tlsTransportCache) get(config *Config) (http.RoundTripper, error) {
110
109
proxy = config .Proxy
111
110
}
112
111
113
- // Cache a single transport for these options
114
- c .transports [key ] = utilnet .SetTransportDefaults (& http.Transport {
112
+ transport := utilnet .SetTransportDefaults (& http.Transport {
115
113
Proxy : proxy ,
116
114
TLSHandshakeTimeout : 10 * time .Second ,
117
115
TLSClientConfig : tlsConfig ,
118
116
MaxIdleConnsPerHost : idleConnsPerHost ,
119
117
DialContext : dial ,
120
118
DisableCompression : config .DisableCompression ,
121
119
})
122
- return c .transports [key ], nil
120
+
121
+ if canCache {
122
+ // Cache a single transport for these options
123
+ c .transports [key ] = transport
124
+ }
125
+
126
+ return transport , nil
123
127
}
124
128
125
129
// tlsConfigKey returns a unique key for tls.Config objects returned from TLSConfigFor
126
- func tlsConfigKey (c * Config ) (tlsCacheKey , error ) {
130
+ func tlsConfigKey (c * Config ) (tlsCacheKey , bool , error ) {
127
131
// Make sure ca/key/cert content is loaded
128
132
if err := loadTLSFiles (c ); err != nil {
129
- return tlsCacheKey {}, err
133
+ return tlsCacheKey {}, false , err
130
134
}
135
+
136
+ if c .TLS .GetCert != nil || c .Dial != nil || c .Proxy != nil {
137
+ // cannot determine equality for functions
138
+ return tlsCacheKey {}, false , nil
139
+ }
140
+
131
141
k := tlsCacheKey {
132
142
insecure : c .TLS .Insecure ,
133
143
caData : string (c .TLS .CAData ),
134
- getCert : fmt .Sprintf ("%p" , c .TLS .GetCert ),
135
144
serverName : c .TLS .ServerName ,
136
145
nextProtos : strings .Join (c .TLS .NextProtos , "," ),
137
- dial : fmt .Sprintf ("%p" , c .Dial ),
138
146
disableCompression : c .DisableCompression ,
139
- proxy : fmt .Sprintf ("%p" , c .Proxy ),
140
147
}
141
148
142
149
if c .TLS .ReloadTLSFiles {
@@ -147,5 +154,5 @@ func tlsConfigKey(c *Config) (tlsCacheKey, error) {
147
154
k .keyData = string (c .TLS .KeyData )
148
155
}
149
156
150
- return k , nil
157
+ return k , true , nil
151
158
}
0 commit comments