gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/balancer/grpclb/grpclb_config.go (about)

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