google.golang.org/grpc@v1.62.1/xds/internal/balancer/priority/config.go (about) 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 19 package priority 20 21 import ( 22 "encoding/json" 23 "fmt" 24 25 internalserviceconfig "google.golang.org/grpc/internal/serviceconfig" 26 "google.golang.org/grpc/serviceconfig" 27 ) 28 29 // Child is a child of priority balancer. 30 type Child struct { 31 Config *internalserviceconfig.BalancerConfig `json:"config,omitempty"` 32 IgnoreReresolutionRequests bool `json:"ignoreReresolutionRequests,omitempty"` 33 } 34 35 // LBConfig represents priority balancer's config. 36 type LBConfig struct { 37 serviceconfig.LoadBalancingConfig `json:"-"` 38 39 // Children is a map from the child balancer names to their configs. Child 40 // names can be found in field Priorities. 41 Children map[string]*Child `json:"children,omitempty"` 42 // Priorities is a list of child balancer names. They are sorted from 43 // highest priority to low. The type/config for each child can be found in 44 // field Children, with the balancer name as the key. 45 Priorities []string `json:"priorities,omitempty"` 46 } 47 48 func parseConfig(c json.RawMessage) (*LBConfig, error) { 49 var cfg LBConfig 50 if err := json.Unmarshal(c, &cfg); err != nil { 51 return nil, err 52 } 53 54 prioritiesSet := make(map[string]bool) 55 for _, name := range cfg.Priorities { 56 if _, ok := cfg.Children[name]; !ok { 57 return nil, fmt.Errorf("LB policy name %q found in Priorities field (%v) is not found in Children field (%+v)", name, cfg.Priorities, cfg.Children) 58 } 59 prioritiesSet[name] = true 60 } 61 for name := range cfg.Children { 62 if _, ok := prioritiesSet[name]; !ok { 63 return nil, fmt.Errorf("LB policy name %q found in Children field (%v) is not found in Priorities field (%+v)", name, cfg.Children, cfg.Priorities) 64 } 65 } 66 return &cfg, nil 67 }