grpclb_util.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. *
  3. * Copyright 2016 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package grpclb
  19. import (
  20. "fmt"
  21. "sync"
  22. "time"
  23. "google.golang.org/grpc/balancer"
  24. "google.golang.org/grpc/resolver"
  25. )
  26. const subConnCacheTime = time.Second * 10
  27. // lbCacheClientConn is a wrapper balancer.ClientConn with a SubConn cache.
  28. // SubConns will be kept in cache for subConnCacheTime before being shut down.
  29. //
  30. // Its NewSubconn and SubConn.Shutdown methods are updated to do cache first.
  31. type lbCacheClientConn struct {
  32. balancer.ClientConn
  33. timeout time.Duration
  34. mu sync.Mutex
  35. // subConnCache only keeps subConns that are being deleted.
  36. subConnCache map[resolver.Address]*subConnCacheEntry
  37. subConnToAddr map[balancer.SubConn]resolver.Address
  38. }
  39. type subConnCacheEntry struct {
  40. sc balancer.SubConn
  41. cancel func()
  42. abortDeleting bool
  43. }
  44. func newLBCacheClientConn(cc balancer.ClientConn) *lbCacheClientConn {
  45. return &lbCacheClientConn{
  46. ClientConn: cc,
  47. timeout: subConnCacheTime,
  48. subConnCache: make(map[resolver.Address]*subConnCacheEntry),
  49. subConnToAddr: make(map[balancer.SubConn]resolver.Address),
  50. }
  51. }
  52. func (ccc *lbCacheClientConn) NewSubConn(addrs []resolver.Address, opts balancer.NewSubConnOptions) (balancer.SubConn, error) {
  53. if len(addrs) != 1 {
  54. return nil, fmt.Errorf("grpclb calling NewSubConn with addrs of length %v", len(addrs))
  55. }
  56. addrWithoutAttrs := addrs[0]
  57. addrWithoutAttrs.Attributes = nil
  58. ccc.mu.Lock()
  59. defer ccc.mu.Unlock()
  60. if entry, ok := ccc.subConnCache[addrWithoutAttrs]; ok {
  61. // If entry is in subConnCache, the SubConn was being deleted.
  62. // cancel function will never be nil.
  63. entry.cancel()
  64. delete(ccc.subConnCache, addrWithoutAttrs)
  65. return entry.sc, nil
  66. }
  67. scNew, err := ccc.ClientConn.NewSubConn(addrs, opts)
  68. if err != nil {
  69. return nil, err
  70. }
  71. scNew = &lbCacheSubConn{SubConn: scNew, ccc: ccc}
  72. ccc.subConnToAddr[scNew] = addrWithoutAttrs
  73. return scNew, nil
  74. }
  75. func (ccc *lbCacheClientConn) RemoveSubConn(sc balancer.SubConn) {
  76. logger.Errorf("RemoveSubConn(%v) called unexpectedly", sc)
  77. }
  78. type lbCacheSubConn struct {
  79. balancer.SubConn
  80. ccc *lbCacheClientConn
  81. }
  82. func (sc *lbCacheSubConn) Shutdown() {
  83. ccc := sc.ccc
  84. ccc.mu.Lock()
  85. defer ccc.mu.Unlock()
  86. addr, ok := ccc.subConnToAddr[sc]
  87. if !ok {
  88. return
  89. }
  90. if entry, ok := ccc.subConnCache[addr]; ok {
  91. if entry.sc != sc {
  92. // This could happen if NewSubConn was called multiple times for
  93. // the same address, and those SubConns are all shut down. We
  94. // remove sc immediately here.
  95. delete(ccc.subConnToAddr, sc)
  96. sc.SubConn.Shutdown()
  97. }
  98. return
  99. }
  100. entry := &subConnCacheEntry{
  101. sc: sc,
  102. }
  103. ccc.subConnCache[addr] = entry
  104. timer := time.AfterFunc(ccc.timeout, func() {
  105. ccc.mu.Lock()
  106. defer ccc.mu.Unlock()
  107. if entry.abortDeleting {
  108. return
  109. }
  110. sc.SubConn.Shutdown()
  111. delete(ccc.subConnToAddr, sc)
  112. delete(ccc.subConnCache, addr)
  113. })
  114. entry.cancel = func() {
  115. if !timer.Stop() {
  116. // If stop was not successful, the timer has fired (this can only
  117. // happen in a race). But the deleting function is blocked on ccc.mu
  118. // because the mutex was held by the caller of this function.
  119. //
  120. // Set abortDeleting to true to abort the deleting function. When
  121. // the lock is released, the deleting function will acquire the
  122. // lock, check the value of abortDeleting and return.
  123. entry.abortDeleting = true
  124. }
  125. }
  126. }
  127. func (ccc *lbCacheClientConn) UpdateState(s balancer.State) {
  128. s.Picker = &lbCachePicker{Picker: s.Picker}
  129. ccc.ClientConn.UpdateState(s)
  130. }
  131. func (ccc *lbCacheClientConn) close() {
  132. ccc.mu.Lock()
  133. defer ccc.mu.Unlock()
  134. // Only cancel all existing timers. There's no need to shut down SubConns.
  135. for _, entry := range ccc.subConnCache {
  136. entry.cancel()
  137. }
  138. }
  139. type lbCachePicker struct {
  140. balancer.Picker
  141. }
  142. func (cp *lbCachePicker) Pick(i balancer.PickInfo) (balancer.PickResult, error) {
  143. res, err := cp.Picker.Pick(i)
  144. if err != nil {
  145. return res, err
  146. }
  147. res.SubConn = res.SubConn.(*lbCacheSubConn).SubConn
  148. return res, nil
  149. }