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