balancer.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2016 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package clientv3
  15. import (
  16. "net/url"
  17. "strings"
  18. "sync"
  19. "golang.org/x/net/context"
  20. "google.golang.org/grpc"
  21. )
  22. // simpleBalancer does the bare minimum to expose multiple eps
  23. // to the grpc reconnection code path
  24. type simpleBalancer struct {
  25. // addrs are the client's endpoints for grpc
  26. addrs []grpc.Address
  27. // notifyCh notifies grpc of the set of addresses for connecting
  28. notifyCh chan []grpc.Address
  29. // readyc closes once the first connection is up
  30. readyc chan struct{}
  31. readyOnce sync.Once
  32. // mu protects upEps, pinAddr, and connectingAddr
  33. mu sync.RWMutex
  34. // upEps holds the current endpoints that have an active connection
  35. upEps map[string]struct{}
  36. // upc closes when upEps transitions from empty to non-zero or the balancer closes.
  37. upc chan struct{}
  38. // pinAddr is the currently pinned address; set to the empty string on
  39. // intialization and shutdown.
  40. pinAddr string
  41. }
  42. func newSimpleBalancer(eps []string) *simpleBalancer {
  43. notifyCh := make(chan []grpc.Address, 1)
  44. addrs := make([]grpc.Address, len(eps))
  45. for i := range eps {
  46. addrs[i].Addr = getHost(eps[i])
  47. }
  48. notifyCh <- addrs
  49. sb := &simpleBalancer{
  50. addrs: addrs,
  51. notifyCh: notifyCh,
  52. readyc: make(chan struct{}),
  53. upEps: make(map[string]struct{}),
  54. upc: make(chan struct{}),
  55. }
  56. return sb
  57. }
  58. func (b *simpleBalancer) Start(target string) error { return nil }
  59. func (b *simpleBalancer) ConnectNotify() <-chan struct{} {
  60. b.mu.Lock()
  61. defer b.mu.Unlock()
  62. return b.upc
  63. }
  64. func (b *simpleBalancer) Up(addr grpc.Address) func(error) {
  65. b.mu.Lock()
  66. if len(b.upEps) == 0 {
  67. // notify waiting Get()s and pin first connected address
  68. close(b.upc)
  69. b.pinAddr = addr.Addr
  70. }
  71. b.upEps[addr.Addr] = struct{}{}
  72. b.mu.Unlock()
  73. // notify client that a connection is up
  74. b.readyOnce.Do(func() { close(b.readyc) })
  75. return func(err error) {
  76. b.mu.Lock()
  77. delete(b.upEps, addr.Addr)
  78. if len(b.upEps) == 0 && b.pinAddr != "" {
  79. b.upc = make(chan struct{})
  80. } else if b.pinAddr == addr.Addr {
  81. // choose new random up endpoint
  82. for k := range b.upEps {
  83. b.pinAddr = k
  84. break
  85. }
  86. }
  87. b.mu.Unlock()
  88. }
  89. }
  90. func (b *simpleBalancer) Get(ctx context.Context, opts grpc.BalancerGetOptions) (grpc.Address, func(), error) {
  91. var addr string
  92. for {
  93. b.mu.RLock()
  94. ch := b.upc
  95. b.mu.RUnlock()
  96. select {
  97. case <-ch:
  98. case <-ctx.Done():
  99. return grpc.Address{Addr: ""}, nil, ctx.Err()
  100. }
  101. b.mu.RLock()
  102. addr = b.pinAddr
  103. upEps := len(b.upEps)
  104. b.mu.RUnlock()
  105. if addr == "" {
  106. return grpc.Address{Addr: ""}, nil, grpc.ErrClientConnClosing
  107. }
  108. if upEps > 0 {
  109. break
  110. }
  111. }
  112. return grpc.Address{Addr: addr}, func() {}, nil
  113. }
  114. func (b *simpleBalancer) Notify() <-chan []grpc.Address { return b.notifyCh }
  115. func (b *simpleBalancer) Close() error {
  116. b.mu.Lock()
  117. close(b.notifyCh)
  118. // terminate all waiting Get()s
  119. b.pinAddr = ""
  120. if len(b.upEps) == 0 {
  121. close(b.upc)
  122. }
  123. b.mu.Unlock()
  124. return nil
  125. }
  126. func getHost(ep string) string {
  127. url, uerr := url.Parse(ep)
  128. if uerr != nil || !strings.Contains(ep, "://") {
  129. return ep
  130. }
  131. return url.Host
  132. }