connrotation.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. *ConnectionTracker
  30. }
  31. // NewDialer creates a new Dialer instance.
  32. // Equivalent to NewDialerWithTracker(dial, nil).
  33. func NewDialer(dial DialFunc) *Dialer {
  34. return NewDialerWithTracker(dial, nil)
  35. }
  36. // NewDialerWithTracker creates a new Dialer instance.
  37. //
  38. // If dial is not nil, it will be used to create new underlying connections.
  39. // Otherwise net.DialContext is used.
  40. // If tracker is not nil, it will be used to track new underlying connections.
  41. // Otherwise NewConnectionTracker() is used.
  42. func NewDialerWithTracker(dial DialFunc, tracker *ConnectionTracker) *Dialer {
  43. if tracker == nil {
  44. tracker = NewConnectionTracker()
  45. }
  46. return &Dialer{
  47. dial: dial,
  48. ConnectionTracker: tracker,
  49. }
  50. }
  51. // ConnectionTracker keeps track of opened connections
  52. type ConnectionTracker struct {
  53. mu sync.Mutex
  54. conns map[*closableConn]struct{}
  55. }
  56. // NewConnectionTracker returns a connection tracker for use with NewDialerWithTracker
  57. func NewConnectionTracker() *ConnectionTracker {
  58. return &ConnectionTracker{
  59. conns: make(map[*closableConn]struct{}),
  60. }
  61. }
  62. // CloseAll forcibly closes all tracked connections.
  63. //
  64. // Note: new connections may get created before CloseAll returns.
  65. func (c *ConnectionTracker) CloseAll() {
  66. c.mu.Lock()
  67. conns := c.conns
  68. c.conns = make(map[*closableConn]struct{})
  69. c.mu.Unlock()
  70. for conn := range conns {
  71. conn.Close()
  72. }
  73. }
  74. // Track adds the connection to the list of tracked connections,
  75. // and returns a wrapped copy of the connection that stops tracking the connection
  76. // when it is closed.
  77. func (c *ConnectionTracker) Track(conn net.Conn) net.Conn {
  78. closable := &closableConn{Conn: conn}
  79. // When the connection is closed, remove it from the map. This will
  80. // be no-op if the connection isn't in the map, e.g. if CloseAll()
  81. // is called.
  82. closable.onClose = func() {
  83. c.mu.Lock()
  84. delete(c.conns, closable)
  85. c.mu.Unlock()
  86. }
  87. // Start tracking the connection
  88. c.mu.Lock()
  89. c.conns[closable] = struct{}{}
  90. c.mu.Unlock()
  91. return closable
  92. }
  93. // Dial creates a new tracked connection.
  94. func (d *Dialer) Dial(network, address string) (net.Conn, error) {
  95. return d.DialContext(context.Background(), network, address)
  96. }
  97. // DialContext creates a new tracked connection.
  98. func (d *Dialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
  99. conn, err := d.dial(ctx, network, address)
  100. if err != nil {
  101. return nil, err
  102. }
  103. return d.ConnectionTracker.Track(conn), nil
  104. }
  105. type closableConn struct {
  106. onClose func()
  107. net.Conn
  108. }
  109. func (c *closableConn) Close() error {
  110. go c.onClose()
  111. return c.Conn.Close()
  112. }