base.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 base defines a balancer base that can be used to build balancers with
  19. // different picking algorithms.
  20. //
  21. // The base balancer creates a new SubConn for each resolved address. The
  22. // provided picker will only be notified about READY SubConns.
  23. //
  24. // This package is the base of round_robin balancer, its purpose is to be used
  25. // to build round_robin like balancers with complex picking algorithms.
  26. // Balancers with more complicated logic should try to implement a balancer
  27. // builder from scratch.
  28. //
  29. // All APIs in this package are experimental.
  30. package base
  31. import (
  32. "google.golang.org/grpc/balancer"
  33. "google.golang.org/grpc/resolver"
  34. )
  35. // PickerBuilder creates balancer.Picker.
  36. type PickerBuilder interface {
  37. // Build takes a slice of ready SubConns, and returns a picker that will be
  38. // used by gRPC to pick a SubConn.
  39. Build(readySCs map[resolver.Address]balancer.SubConn) balancer.Picker
  40. }
  41. // V2PickerBuilder creates balancer.V2Picker.
  42. type V2PickerBuilder interface {
  43. // Build returns a picker that will be used by gRPC to pick a SubConn.
  44. Build(info PickerBuildInfo) balancer.V2Picker
  45. }
  46. // PickerBuildInfo contains information needed by the picker builder to
  47. // construct a picker.
  48. type PickerBuildInfo struct {
  49. // ReadySCs is a map from all ready SubConns to the Addresses used to
  50. // create them.
  51. ReadySCs map[balancer.SubConn]SubConnInfo
  52. }
  53. // SubConnInfo contains information about a SubConn created by the base
  54. // balancer.
  55. type SubConnInfo struct {
  56. Address resolver.Address // the address used to create this SubConn
  57. }
  58. // NewBalancerBuilder returns a balancer builder. The balancers
  59. // built by this builder will use the picker builder to build pickers.
  60. func NewBalancerBuilder(name string, pb PickerBuilder) balancer.Builder {
  61. return NewBalancerBuilderWithConfig(name, pb, Config{})
  62. }
  63. // Config contains the config info about the base balancer builder.
  64. type Config struct {
  65. // HealthCheck indicates whether health checking should be enabled for this specific balancer.
  66. HealthCheck bool
  67. }
  68. // NewBalancerBuilderWithConfig returns a base balancer builder configured by the provided config.
  69. func NewBalancerBuilderWithConfig(name string, pb PickerBuilder, config Config) balancer.Builder {
  70. return &baseBuilder{
  71. name: name,
  72. pickerBuilder: pb,
  73. config: config,
  74. }
  75. }
  76. // NewBalancerBuilderV2 returns a base balancer builder configured by the provided config.
  77. func NewBalancerBuilderV2(name string, pb V2PickerBuilder, config Config) balancer.Builder {
  78. return &baseBuilder{
  79. name: name,
  80. v2PickerBuilder: pb,
  81. config: config,
  82. }
  83. }