stub.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. *
  3. * Copyright 2020 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 stub implements a balancer for testing purposes.
  19. package stub
  20. import (
  21. "encoding/json"
  22. "google.golang.org/grpc/balancer"
  23. "google.golang.org/grpc/serviceconfig"
  24. )
  25. // BalancerFuncs contains all balancer.Balancer functions with a preceding
  26. // *BalancerData parameter for passing additional instance information. Any
  27. // nil functions will never be called.
  28. type BalancerFuncs struct {
  29. // Init is called after ClientConn and BuildOptions are set in
  30. // BalancerData. It may be used to initialize BalancerData.Data.
  31. Init func(*BalancerData)
  32. // ParseConfig is used for parsing LB configs, if specified.
  33. ParseConfig func(json.RawMessage) (serviceconfig.LoadBalancingConfig, error)
  34. UpdateClientConnState func(*BalancerData, balancer.ClientConnState) error
  35. ResolverError func(*BalancerData, error)
  36. UpdateSubConnState func(*BalancerData, balancer.SubConn, balancer.SubConnState)
  37. Close func(*BalancerData)
  38. ExitIdle func(*BalancerData)
  39. }
  40. // BalancerData contains data relevant to a stub balancer.
  41. type BalancerData struct {
  42. // ClientConn is set by the builder.
  43. ClientConn balancer.ClientConn
  44. // BuildOptions is set by the builder.
  45. BuildOptions balancer.BuildOptions
  46. // Data may be used to store arbitrary user data.
  47. Data interface{}
  48. }
  49. type bal struct {
  50. bf BalancerFuncs
  51. bd *BalancerData
  52. }
  53. func (b *bal) UpdateClientConnState(c balancer.ClientConnState) error {
  54. if b.bf.UpdateClientConnState != nil {
  55. return b.bf.UpdateClientConnState(b.bd, c)
  56. }
  57. return nil
  58. }
  59. func (b *bal) ResolverError(e error) {
  60. if b.bf.ResolverError != nil {
  61. b.bf.ResolverError(b.bd, e)
  62. }
  63. }
  64. func (b *bal) UpdateSubConnState(sc balancer.SubConn, scs balancer.SubConnState) {
  65. if b.bf.UpdateSubConnState != nil {
  66. b.bf.UpdateSubConnState(b.bd, sc, scs)
  67. }
  68. }
  69. func (b *bal) Close() {
  70. if b.bf.Close != nil {
  71. b.bf.Close(b.bd)
  72. }
  73. }
  74. func (b *bal) ExitIdle() {
  75. if b.bf.ExitIdle != nil {
  76. b.bf.ExitIdle(b.bd)
  77. }
  78. }
  79. type bb struct {
  80. name string
  81. bf BalancerFuncs
  82. }
  83. func (bb bb) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer {
  84. b := &bal{bf: bb.bf, bd: &BalancerData{ClientConn: cc, BuildOptions: opts}}
  85. if b.bf.Init != nil {
  86. b.bf.Init(b.bd)
  87. }
  88. return b
  89. }
  90. func (bb bb) Name() string { return bb.name }
  91. func (bb bb) ParseConfig(lbCfg json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {
  92. if bb.bf.ParseConfig != nil {
  93. return bb.bf.ParseConfig(lbCfg)
  94. }
  95. return nil, nil
  96. }
  97. // Register registers a stub balancer builder which will call the provided
  98. // functions. The name used should be unique.
  99. func Register(name string, bf BalancerFuncs) {
  100. balancer.Register(bb{name: name, bf: bf})
  101. }