pickfirst.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. "golang.org/x/net/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. grpclog.Infof("pickfirstBalancer: HandleResolvedAddrs called with error %v", err)
  45. return
  46. }
  47. if b.sc == nil {
  48. b.sc, err = b.cc.NewSubConn(addrs, balancer.NewSubConnOptions{})
  49. if err != nil {
  50. grpclog.Errorf("pickfirstBalancer: failed to NewSubConn: %v", err)
  51. return
  52. }
  53. b.cc.UpdateBalancerState(connectivity.Idle, &picker{sc: b.sc})
  54. b.sc.Connect()
  55. } else {
  56. b.sc.UpdateAddresses(addrs)
  57. b.sc.Connect()
  58. }
  59. }
  60. func (b *pickfirstBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
  61. grpclog.Infof("pickfirstBalancer: HandleSubConnStateChange: %p, %v", sc, s)
  62. if b.sc != sc {
  63. grpclog.Infof("pickfirstBalancer: ignored state change because sc is not recognized")
  64. return
  65. }
  66. if s == connectivity.Shutdown {
  67. b.sc = nil
  68. return
  69. }
  70. switch s {
  71. case connectivity.Ready, connectivity.Idle:
  72. b.cc.UpdateBalancerState(s, &picker{sc: sc})
  73. case connectivity.Connecting:
  74. b.cc.UpdateBalancerState(s, &picker{err: balancer.ErrNoSubConnAvailable})
  75. case connectivity.TransientFailure:
  76. b.cc.UpdateBalancerState(s, &picker{err: balancer.ErrTransientFailure})
  77. }
  78. }
  79. func (b *pickfirstBalancer) Close() {
  80. }
  81. type picker struct {
  82. err error
  83. sc balancer.SubConn
  84. }
  85. func (p *picker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) {
  86. if p.err != nil {
  87. return nil, nil, p.err
  88. }
  89. return p.sc, nil, nil
  90. }
  91. func init() {
  92. balancer.Register(newPickfirstBuilder())
  93. }