picker_wrapper.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. *
  3. * Copyright 2017 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 grpc
  19. import (
  20. "sync"
  21. "golang.org/x/net/context"
  22. "google.golang.org/grpc/balancer"
  23. "google.golang.org/grpc/codes"
  24. "google.golang.org/grpc/grpclog"
  25. "google.golang.org/grpc/status"
  26. "google.golang.org/grpc/transport"
  27. )
  28. // pickerWrapper is a wrapper of balancer.Picker. It blocks on certain pick
  29. // actions and unblock when there's a picker update.
  30. type pickerWrapper struct {
  31. mu sync.Mutex
  32. done bool
  33. blockingCh chan struct{}
  34. picker balancer.Picker
  35. }
  36. func newPickerWrapper() *pickerWrapper {
  37. bp := &pickerWrapper{blockingCh: make(chan struct{})}
  38. return bp
  39. }
  40. // updatePicker is called by UpdateBalancerState. It unblocks all blocked pick.
  41. func (bp *pickerWrapper) updatePicker(p balancer.Picker) {
  42. bp.mu.Lock()
  43. if bp.done {
  44. bp.mu.Unlock()
  45. return
  46. }
  47. bp.picker = p
  48. // bp.blockingCh should never be nil.
  49. close(bp.blockingCh)
  50. bp.blockingCh = make(chan struct{})
  51. bp.mu.Unlock()
  52. }
  53. // pick returns the transport that will be used for the RPC.
  54. // It may block in the following cases:
  55. // - there's no picker
  56. // - the current picker returns ErrNoSubConnAvailable
  57. // - the current picker returns other errors and failfast is false.
  58. // - the subConn returned by the current picker is not READY
  59. // When one of these situations happens, pick blocks until the picker gets updated.
  60. func (bp *pickerWrapper) pick(ctx context.Context, failfast bool, opts balancer.PickOptions) (transport.ClientTransport, func(balancer.DoneInfo), error) {
  61. var (
  62. p balancer.Picker
  63. ch chan struct{}
  64. )
  65. for {
  66. bp.mu.Lock()
  67. if bp.done {
  68. bp.mu.Unlock()
  69. return nil, nil, ErrClientConnClosing
  70. }
  71. if bp.picker == nil {
  72. ch = bp.blockingCh
  73. }
  74. if ch == bp.blockingCh {
  75. // This could happen when either:
  76. // - bp.picker is nil (the previous if condition), or
  77. // - has called pick on the current picker.
  78. bp.mu.Unlock()
  79. select {
  80. case <-ctx.Done():
  81. return nil, nil, ctx.Err()
  82. case <-ch:
  83. }
  84. continue
  85. }
  86. ch = bp.blockingCh
  87. p = bp.picker
  88. bp.mu.Unlock()
  89. subConn, put, err := p.Pick(ctx, opts)
  90. if err != nil {
  91. switch err {
  92. case balancer.ErrNoSubConnAvailable:
  93. continue
  94. case balancer.ErrTransientFailure:
  95. if !failfast {
  96. continue
  97. }
  98. return nil, nil, status.Errorf(codes.Unavailable, "%v", err)
  99. default:
  100. // err is some other error.
  101. return nil, nil, toRPCErr(err)
  102. }
  103. }
  104. acw, ok := subConn.(*acBalancerWrapper)
  105. if !ok {
  106. grpclog.Infof("subconn returned from pick is not *acBalancerWrapper")
  107. continue
  108. }
  109. if t, ok := acw.getAddrConn().getReadyTransport(); ok {
  110. return t, put, nil
  111. }
  112. grpclog.Infof("blockingPicker: the picked transport is not ready, loop back to repick")
  113. // If ok == false, ac.state is not READY.
  114. // A valid picker always returns READY subConn. This means the state of ac
  115. // just changed, and picker will be updated shortly.
  116. // continue back to the beginning of the for loop to repick.
  117. }
  118. }
  119. func (bp *pickerWrapper) close() {
  120. bp.mu.Lock()
  121. defer bp.mu.Unlock()
  122. if bp.done {
  123. return
  124. }
  125. bp.done = true
  126. close(bp.blockingCh)
  127. }