pickfirst.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. "errors"
  21. "fmt"
  22. "google.golang.org/grpc/balancer"
  23. "google.golang.org/grpc/connectivity"
  24. )
  25. // PickFirstBalancerName is the name of the pick_first balancer.
  26. const PickFirstBalancerName = "pick_first"
  27. func newPickfirstBuilder() balancer.Builder {
  28. return &pickfirstBuilder{}
  29. }
  30. type pickfirstBuilder struct{}
  31. func (*pickfirstBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
  32. return &pickfirstBalancer{cc: cc}
  33. }
  34. func (*pickfirstBuilder) Name() string {
  35. return PickFirstBalancerName
  36. }
  37. type pickfirstBalancer struct {
  38. state connectivity.State
  39. cc balancer.ClientConn
  40. sc balancer.SubConn
  41. }
  42. func (b *pickfirstBalancer) ResolverError(err error) {
  43. switch b.state {
  44. case connectivity.TransientFailure, connectivity.Idle, connectivity.Connecting:
  45. // Set a failing picker if we don't have a good picker.
  46. b.cc.UpdateState(balancer.State{ConnectivityState: connectivity.TransientFailure,
  47. Picker: &picker{err: fmt.Errorf("name resolver error: %v", err)},
  48. })
  49. }
  50. if logger.V(2) {
  51. logger.Infof("pickfirstBalancer: ResolverError called with error %v", err)
  52. }
  53. }
  54. func (b *pickfirstBalancer) UpdateClientConnState(cs balancer.ClientConnState) error {
  55. if len(cs.ResolverState.Addresses) == 0 {
  56. b.ResolverError(errors.New("produced zero addresses"))
  57. return balancer.ErrBadResolverState
  58. }
  59. if b.sc == nil {
  60. var err error
  61. b.sc, err = b.cc.NewSubConn(cs.ResolverState.Addresses, balancer.NewSubConnOptions{})
  62. if err != nil {
  63. if logger.V(2) {
  64. logger.Errorf("pickfirstBalancer: failed to NewSubConn: %v", err)
  65. }
  66. b.state = connectivity.TransientFailure
  67. b.cc.UpdateState(balancer.State{ConnectivityState: connectivity.TransientFailure,
  68. Picker: &picker{err: fmt.Errorf("error creating connection: %v", err)},
  69. })
  70. return balancer.ErrBadResolverState
  71. }
  72. b.state = connectivity.Idle
  73. b.cc.UpdateState(balancer.State{ConnectivityState: connectivity.Idle, Picker: &picker{result: balancer.PickResult{SubConn: b.sc}}})
  74. b.sc.Connect()
  75. } else {
  76. b.cc.UpdateAddresses(b.sc, cs.ResolverState.Addresses)
  77. b.sc.Connect()
  78. }
  79. return nil
  80. }
  81. func (b *pickfirstBalancer) UpdateSubConnState(sc balancer.SubConn, s balancer.SubConnState) {
  82. if logger.V(2) {
  83. logger.Infof("pickfirstBalancer: UpdateSubConnState: %p, %v", sc, s)
  84. }
  85. if b.sc != sc {
  86. if logger.V(2) {
  87. logger.Infof("pickfirstBalancer: ignored state change because sc is not recognized")
  88. }
  89. return
  90. }
  91. b.state = s.ConnectivityState
  92. if s.ConnectivityState == connectivity.Shutdown {
  93. b.sc = nil
  94. return
  95. }
  96. switch s.ConnectivityState {
  97. case connectivity.Ready, connectivity.Idle:
  98. b.cc.UpdateState(balancer.State{ConnectivityState: s.ConnectivityState, Picker: &picker{result: balancer.PickResult{SubConn: sc}}})
  99. case connectivity.Connecting:
  100. b.cc.UpdateState(balancer.State{ConnectivityState: s.ConnectivityState, Picker: &picker{err: balancer.ErrNoSubConnAvailable}})
  101. case connectivity.TransientFailure:
  102. b.cc.UpdateState(balancer.State{
  103. ConnectivityState: s.ConnectivityState,
  104. Picker: &picker{err: s.ConnectionError},
  105. })
  106. }
  107. }
  108. func (b *pickfirstBalancer) Close() {
  109. }
  110. type picker struct {
  111. result balancer.PickResult
  112. err error
  113. }
  114. func (p *picker) Pick(info balancer.PickInfo) (balancer.PickResult, error) {
  115. return p.result, p.err
  116. }
  117. func init() {
  118. balancer.Register(newPickfirstBuilder())
  119. }