connrotation.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. Copyright 2018 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 connrotation implements a connection dialer that tracks and can close
  14. // all created connections.
  15. //
  16. // This is used for credential rotation of long-lived connections, when there's
  17. // no way to re-authenticate on a live connection.
  18. package connrotation
  19. import (
  20. "context"
  21. "net"
  22. "sync"
  23. )
  24. // DialFunc is a shorthand for signature of net.DialContext.
  25. type DialFunc func(ctx context.Context, network, address string) (net.Conn, error)
  26. // Dialer opens connections through Dial and tracks them.
  27. type Dialer struct {
  28. dial DialFunc
  29. mu sync.Mutex
  30. conns map[*closableConn]struct{}
  31. }
  32. // NewDialer creates a new Dialer instance.
  33. //
  34. // If dial is not nil, it will be used to create new underlying connections.
  35. // Otherwise net.DialContext is used.
  36. func NewDialer(dial DialFunc) *Dialer {
  37. return &Dialer{
  38. dial: dial,
  39. conns: make(map[*closableConn]struct{}),
  40. }
  41. }
  42. // CloseAll forcibly closes all tracked connections.
  43. //
  44. // Note: new connections may get created before CloseAll returns.
  45. func (d *Dialer) CloseAll() {
  46. d.mu.Lock()
  47. conns := d.conns
  48. d.conns = make(map[*closableConn]struct{})
  49. d.mu.Unlock()
  50. for conn := range conns {
  51. conn.Close()
  52. }
  53. }
  54. // Dial creates a new tracked connection.
  55. func (d *Dialer) Dial(network, address string) (net.Conn, error) {
  56. return d.DialContext(context.Background(), network, address)
  57. }
  58. // DialContext creates a new tracked connection.
  59. func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
  60. conn, err := d.dial(ctx, network, address)
  61. if err != nil {
  62. return nil, err
  63. }
  64. closable := &closableConn{Conn: conn}
  65. // Start tracking the connection
  66. d.mu.Lock()
  67. d.conns[closable] = struct{}{}
  68. d.mu.Unlock()
  69. // When the connection is closed, remove it from the map. This will
  70. // be no-op if the connection isn't in the map, e.g. if CloseAll()
  71. // is called.
  72. closable.onClose = func() {
  73. d.mu.Lock()
  74. delete(d.conns, closable)
  75. d.mu.Unlock()
  76. }
  77. return closable, nil
  78. }
  79. type closableConn struct {
  80. onClose func()
  81. net.Conn
  82. }
  83. func (c *closableConn) Close() error {
  84. go c.onClose()
  85. return c.Conn.Close()
  86. }