pickfirst.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "context"
  21. "google.golang.org/grpc/balancer"
  22. "google.golang.org/grpc/connectivity"
  23. "google.golang.org/grpc/grpclog"
  24. "google.golang.org/grpc/resolver"
  25. )
  26. // PickFirstBalancerName is the name of the pick_first balancer.
  27. const PickFirstBalancerName = "pick_first"
  28. func newPickfirstBuilder() balancer.Builder {
  29. return &pickfirstBuilder{}
  30. }
  31. type pickfirstBuilder struct{}
  32. func (*pickfirstBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
  33. return &pickfirstBalancer{cc: cc}
  34. }
  35. func (*pickfirstBuilder) Name() string {
  36. return PickFirstBalancerName
  37. }
  38. type pickfirstBalancer struct {
  39. cc balancer.ClientConn
  40. sc balancer.SubConn
  41. }
  42. func (b *pickfirstBalancer) HandleResolvedAddrs(addrs []resolver.Address, err error) {
  43. if err != nil {
  44. if grpclog.V(2) {
  45. grpclog.Infof("pickfirstBalancer: HandleResolvedAddrs called with error %v", err)
  46. }
  47. return
  48. }
  49. if b.sc == nil {
  50. b.sc, err = b.cc.NewSubConn(addrs, balancer.NewSubConnOptions{})
  51. if err != nil {
  52. //TODO(yuxuanli): why not change the cc state to Idle?
  53. if grpclog.V(2) {
  54. grpclog.Errorf("pickfirstBalancer: failed to NewSubConn: %v", err)
  55. }
  56. return
  57. }
  58. b.cc.UpdateBalancerState(connectivity.Idle, &picker{sc: b.sc})
  59. b.sc.Connect()
  60. } else {
  61. b.sc.UpdateAddresses(addrs)
  62. b.sc.Connect()
  63. }
  64. }
  65. func (b *pickfirstBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
  66. if grpclog.V(2) {
  67. grpclog.Infof("pickfirstBalancer: HandleSubConnStateChange: %p, %v", sc, s)
  68. }
  69. if b.sc != sc {
  70. if grpclog.V(2) {
  71. grpclog.Infof("pickfirstBalancer: ignored state change because sc is not recognized")
  72. }
  73. return
  74. }
  75. if s == connectivity.Shutdown {
  76. b.sc = nil
  77. return
  78. }
  79. switch s {
  80. case connectivity.Ready, connectivity.Idle:
  81. b.cc.UpdateBalancerState(s, &picker{sc: sc})
  82. case connectivity.Connecting:
  83. b.cc.UpdateBalancerState(s, &picker{err: balancer.ErrNoSubConnAvailable})
  84. case connectivity.TransientFailure:
  85. b.cc.UpdateBalancerState(s, &picker{err: balancer.ErrTransientFailure})
  86. }
  87. }
  88. func (b *pickfirstBalancer) Close() {
  89. }
  90. type picker struct {
  91. err error
  92. sc balancer.SubConn
  93. }
  94. func (p *picker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) {
  95. if p.err != nil {
  96. return nil, nil, p.err
  97. }
  98. return p.sc, nil, nil
  99. }
  100. func init() {
  101. balancer.Register(newPickfirstBuilder())
  102. }