pickfirst.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. func newPickfirstBuilder() balancer.Builder {
  27. return &pickfirstBuilder{}
  28. }
  29. type pickfirstBuilder struct{}
  30. func (*pickfirstBuilder) Build(cc balancer.ClientConn, opt balancer.BuildOptions) balancer.Balancer {
  31. return &pickfirstBalancer{cc: cc}
  32. }
  33. func (*pickfirstBuilder) Name() string {
  34. return "pickfirst"
  35. }
  36. type pickfirstBalancer struct {
  37. cc balancer.ClientConn
  38. sc balancer.SubConn
  39. }
  40. func (b *pickfirstBalancer) HandleResolvedAddrs(addrs []resolver.Address, err error) {
  41. if err != nil {
  42. grpclog.Infof("pickfirstBalancer: HandleResolvedAddrs called with error %v", err)
  43. return
  44. }
  45. if b.sc == nil {
  46. b.sc, err = b.cc.NewSubConn(addrs, balancer.NewSubConnOptions{})
  47. if err != nil {
  48. grpclog.Errorf("pickfirstBalancer: failed to NewSubConn: %v", err)
  49. return
  50. }
  51. b.cc.UpdateBalancerState(connectivity.Idle, &picker{sc: b.sc})
  52. } else {
  53. b.sc.UpdateAddresses(addrs)
  54. }
  55. }
  56. func (b *pickfirstBalancer) HandleSubConnStateChange(sc balancer.SubConn, s connectivity.State) {
  57. grpclog.Infof("pickfirstBalancer: HandleSubConnStateChange: %p, %v", sc, s)
  58. if b.sc != sc || s == connectivity.Shutdown {
  59. b.sc = nil
  60. return
  61. }
  62. switch s {
  63. case connectivity.Ready, connectivity.Idle:
  64. b.cc.UpdateBalancerState(s, &picker{sc: sc})
  65. case connectivity.Connecting:
  66. b.cc.UpdateBalancerState(s, &picker{err: balancer.ErrNoSubConnAvailable})
  67. case connectivity.TransientFailure:
  68. b.cc.UpdateBalancerState(s, &picker{err: balancer.ErrTransientFailure})
  69. }
  70. }
  71. func (b *pickfirstBalancer) Close() {
  72. }
  73. type picker struct {
  74. err error
  75. sc balancer.SubConn
  76. }
  77. func (p *picker) Pick(ctx context.Context, opts balancer.PickOptions) (balancer.SubConn, func(balancer.DoneInfo), error) {
  78. if p.err != nil {
  79. return nil, nil, p.err
  80. }
  81. return p.sc, nil, nil
  82. }