github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/balancer/weightedtarget/weightedtarget_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 weightedtarget
    20  
    21  import (
    22  	"encoding/json"
    23  
    24  	internalserviceconfig "github.com/hxx258456/ccgo/grpc/internal/serviceconfig"
    25  	"github.com/hxx258456/ccgo/grpc/serviceconfig"
    26  )
    27  
    28  // Target represents one target with the weight and the child policy.
    29  type Target struct {
    30  	// Weight is the weight of the child policy.
    31  	Weight uint32 `json:"weight,omitempty"`
    32  	// ChildPolicy is the child policy and it's config.
    33  	ChildPolicy *internalserviceconfig.BalancerConfig `json:"childPolicy,omitempty"`
    34  }
    35  
    36  // LBConfig is the balancer config for weighted_target.
    37  type LBConfig struct {
    38  	serviceconfig.LoadBalancingConfig `json:"-"`
    39  
    40  	Targets map[string]Target `json:"targets,omitempty"`
    41  }
    42  
    43  func parseConfig(c json.RawMessage) (*LBConfig, error) {
    44  	var cfg LBConfig
    45  	if err := json.Unmarshal(c, &cfg); err != nil {
    46  		return nil, err
    47  	}
    48  	return &cfg, nil
    49  }