grpclb_config.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. *
  3. * Copyright 2019 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 grpclb
  19. import (
  20. "encoding/json"
  21. "google.golang.org/grpc"
  22. "google.golang.org/grpc/balancer/roundrobin"
  23. "google.golang.org/grpc/serviceconfig"
  24. )
  25. const (
  26. roundRobinName = roundrobin.Name
  27. pickFirstName = grpc.PickFirstBalancerName
  28. )
  29. type grpclbServiceConfig struct {
  30. serviceconfig.LoadBalancingConfig
  31. ChildPolicy *[]map[string]json.RawMessage
  32. ServiceName string
  33. }
  34. func (b *lbBuilder) ParseConfig(lbConfig json.RawMessage) (serviceconfig.LoadBalancingConfig, error) {
  35. ret := &grpclbServiceConfig{}
  36. if err := json.Unmarshal(lbConfig, ret); err != nil {
  37. return nil, err
  38. }
  39. return ret, nil
  40. }
  41. func childIsPickFirst(sc *grpclbServiceConfig) bool {
  42. if sc == nil {
  43. return false
  44. }
  45. childConfigs := sc.ChildPolicy
  46. if childConfigs == nil {
  47. return false
  48. }
  49. for _, childC := range *childConfigs {
  50. // If round_robin exists before pick_first, return false
  51. if _, ok := childC[roundRobinName]; ok {
  52. return false
  53. }
  54. // If pick_first is before round_robin, return true
  55. if _, ok := childC[pickFirstName]; ok {
  56. return true
  57. }
  58. }
  59. return false
  60. }