@@ -47,10 +47,8 @@ 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
53
}
56
54
@@ -59,22 +57,24 @@ func (t tlsCacheKey) String() string {
59
57
if len (t .keyData ) > 0 {
60
58
keyText = "<redacted>"
61
59
}
62
- return fmt .Sprintf ("insecure:%v, caData:%#v, certData:%#v, keyData:%s, getCert: %s, serverName:%s, dial:%s disableCompression:%t" , t .insecure , t .caData , t .certData , keyText , t .getCert , t . serverName , t . dial , t .disableCompression )
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 )
63
61
}
64
62
65
63
func (c * tlsTransportCache ) get (config * Config ) (http.RoundTripper , error ) {
66
- key , err := tlsConfigKey (config )
64
+ key , canCache , err := tlsConfigKey (config )
67
65
if err != nil {
68
66
return nil , err
69
67
}
70
68
71
- // Ensure we only create a single transport for the given TLS options
72
- c .mu .Lock ()
73
- 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 ()
74
73
75
- // See if we already have a custom transport for this config
76
- if t , ok := c .transports [key ]; ok {
77
- 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
+ }
78
78
}
79
79
80
80
// Get the TLS options for this client config
@@ -104,31 +104,40 @@ func (c *tlsTransportCache) get(config *Config) (http.RoundTripper, error) {
104
104
go dynamicCertDialer .Run (wait .NeverStop )
105
105
}
106
106
107
- // Cache a single transport for these options
108
- c .transports [key ] = utilnet .SetTransportDefaults (& http.Transport {
107
+ transport := utilnet .SetTransportDefaults (& http.Transport {
109
108
Proxy : http .ProxyFromEnvironment ,
110
109
TLSHandshakeTimeout : 10 * time .Second ,
111
110
TLSClientConfig : tlsConfig ,
112
111
MaxIdleConnsPerHost : idleConnsPerHost ,
113
112
DialContext : dial ,
114
113
DisableCompression : config .DisableCompression ,
115
114
})
116
- return c .transports [key ], nil
115
+
116
+ if canCache {
117
+ // Cache a single transport for these options
118
+ c .transports [key ] = transport
119
+ }
120
+
121
+ return transport , nil
117
122
}
118
123
119
124
// tlsConfigKey returns a unique key for tls.Config objects returned from TLSConfigFor
120
- func tlsConfigKey (c * Config ) (tlsCacheKey , error ) {
125
+ func tlsConfigKey (c * Config ) (tlsCacheKey , bool , error ) {
121
126
// Make sure ca/key/cert content is loaded
122
127
if err := loadTLSFiles (c ); err != nil {
123
- return tlsCacheKey {}, err
128
+ return tlsCacheKey {}, false , err
124
129
}
130
+
131
+ if c .TLS .GetCert != nil || c .Dial != nil {
132
+ // cannot determine equality for functions
133
+ return tlsCacheKey {}, false , nil
134
+ }
135
+
125
136
k := tlsCacheKey {
126
137
insecure : c .TLS .Insecure ,
127
138
caData : string (c .TLS .CAData ),
128
- getCert : fmt .Sprintf ("%p" , c .TLS .GetCert ),
129
139
serverName : c .TLS .ServerName ,
130
140
nextProtos : strings .Join (c .TLS .NextProtos , "," ),
131
- dial : fmt .Sprintf ("%p" , c .Dial ),
132
141
disableCompression : c .DisableCompression ,
133
142
}
134
143
@@ -140,5 +149,5 @@ func tlsConfigKey(c *Config) (tlsCacheKey, error) {
140
149
k .keyData = string (c .TLS .KeyData )
141
150
}
142
151
143
- return k , nil
152
+ return k , true , nil
144
153
}
0 commit comments