Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

111 wiersze
2.8 KiB

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