@@ -44,10 +44,8 @@ type tlsCacheKey struct {
44
44
caData string
45
45
certData string
46
46
keyData string
47
- getCert string
48
47
serverName string
49
48
nextProtos string
50
- dial string
51
49
disableCompression bool
52
50
}
53
51
@@ -56,22 +54,24 @@ func (t tlsCacheKey) String() string {
56
54
if len (t .keyData ) > 0 {
57
55
keyText = "<redacted>"
58
56
}
59
- 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 )
57
+ 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 )
60
58
}
61
59
62
60
func (c * tlsTransportCache ) get (config * Config ) (http.RoundTripper , error ) {
63
- key , err := tlsConfigKey (config )
61
+ key , canCache , err := tlsConfigKey (config )
64
62
if err != nil {
65
63
return nil , err
66
64
}
67
65
68
- // Ensure we only create a single transport for the given TLS options
69
- c .mu .Lock ()
70
- defer c .mu .Unlock ()
66
+ if canCache {
67
+ // Ensure we only create a single transport for the given TLS options
68
+ c .mu .Lock ()
69
+ defer c .mu .Unlock ()
71
70
72
- // See if we already have a custom transport for this config
73
- if t , ok := c .transports [key ]; ok {
74
- return t , nil
71
+ // See if we already have a custom transport for this config
72
+ if t , ok := c .transports [key ]; ok {
73
+ return t , nil
74
+ }
75
75
}
76
76
77
77
// Get the TLS options for this client config
@@ -91,33 +91,42 @@ func (c *tlsTransportCache) get(config *Config) (http.RoundTripper, error) {
91
91
KeepAlive : 30 * time .Second ,
92
92
}).DialContext
93
93
}
94
- // Cache a single transport for these options
95
- c .transports [key ] = utilnet .SetTransportDefaults (& http.Transport {
94
+ transport := utilnet .SetTransportDefaults (& http.Transport {
96
95
Proxy : http .ProxyFromEnvironment ,
97
96
TLSHandshakeTimeout : 10 * time .Second ,
98
97
TLSClientConfig : tlsConfig ,
99
98
MaxIdleConnsPerHost : idleConnsPerHost ,
100
99
DialContext : dial ,
101
100
DisableCompression : config .DisableCompression ,
102
101
})
103
- return c .transports [key ], nil
102
+
103
+ if canCache {
104
+ // Cache a single transport for these options
105
+ c .transports [key ] = transport
106
+ }
107
+
108
+ return transport , nil
104
109
}
105
110
106
111
// tlsConfigKey returns a unique key for tls.Config objects returned from TLSConfigFor
107
- func tlsConfigKey (c * Config ) (tlsCacheKey , error ) {
112
+ func tlsConfigKey (c * Config ) (tlsCacheKey , bool , error ) {
108
113
// Make sure ca/key/cert content is loaded
109
114
if err := loadTLSFiles (c ); err != nil {
110
- return tlsCacheKey {}, err
115
+ return tlsCacheKey {}, false , err
111
116
}
117
+
118
+ if c .TLS .GetCert != nil || c .Dial != nil {
119
+ // cannot determine equality for functions
120
+ return tlsCacheKey {}, false , nil
121
+ }
122
+
112
123
return tlsCacheKey {
113
124
insecure : c .TLS .Insecure ,
114
125
caData : string (c .TLS .CAData ),
115
126
certData : string (c .TLS .CertData ),
116
127
keyData : string (c .TLS .KeyData ),
117
- getCert : fmt .Sprintf ("%p" , c .TLS .GetCert ),
118
128
serverName : c .TLS .ServerName ,
119
129
nextProtos : strings .Join (c .TLS .NextProtos , "," ),
120
- dial : fmt .Sprintf ("%p" , c .Dial ),
121
130
disableCompression : c .DisableCompression ,
122
- }, nil
131
+ }, true , nil
123
132
}
0 commit comments