cache.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Copyright 2015 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package transport
  14. import (
  15. "fmt"
  16. "net"
  17. "net/http"
  18. "sync"
  19. "time"
  20. utilnet "k8s.io/apimachinery/pkg/util/net"
  21. )
  22. // TlsTransportCache caches TLS http.RoundTrippers different configurations. The
  23. // same RoundTripper will be returned for configs with identical TLS options If
  24. // the config has no custom TLS options, http.DefaultTransport is returned.
  25. type tlsTransportCache struct {
  26. mu sync.Mutex
  27. transports map[string]*http.Transport
  28. }
  29. const idleConnsPerHost = 25
  30. var tlsCache = &tlsTransportCache{transports: make(map[string]*http.Transport)}
  31. func (c *tlsTransportCache) get(config *Config) (http.RoundTripper, error) {
  32. key, err := tlsConfigKey(config)
  33. if err != nil {
  34. return nil, err
  35. }
  36. // Ensure we only create a single transport for the given TLS options
  37. c.mu.Lock()
  38. defer c.mu.Unlock()
  39. // See if we already have a custom transport for this config
  40. if t, ok := c.transports[key]; ok {
  41. return t, nil
  42. }
  43. // Get the TLS options for this client config
  44. tlsConfig, err := TLSConfigFor(config)
  45. if err != nil {
  46. return nil, err
  47. }
  48. // The options didn't require a custom TLS config
  49. if tlsConfig == nil {
  50. return http.DefaultTransport, nil
  51. }
  52. // Cache a single transport for these options
  53. c.transports[key] = utilnet.SetTransportDefaults(&http.Transport{
  54. Proxy: http.ProxyFromEnvironment,
  55. TLSHandshakeTimeout: 10 * time.Second,
  56. TLSClientConfig: tlsConfig,
  57. MaxIdleConnsPerHost: idleConnsPerHost,
  58. Dial: (&net.Dialer{
  59. Timeout: 30 * time.Second,
  60. KeepAlive: 30 * time.Second,
  61. }).Dial,
  62. })
  63. return c.transports[key], nil
  64. }
  65. // tlsConfigKey returns a unique key for tls.Config objects returned from TLSConfigFor
  66. func tlsConfigKey(c *Config) (string, error) {
  67. // Make sure ca/key/cert content is loaded
  68. if err := loadTLSFiles(c); err != nil {
  69. return "", err
  70. }
  71. // Only include the things that actually affect the tls.Config
  72. return fmt.Sprintf("%v/%x/%x/%x", c.TLS.Insecure, c.TLS.CAData, c.TLS.CertData, c.TLS.KeyData), nil
  73. }