github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/xds/internal/balancer/ringhash/config.go (about)

     1  /*
     2   *
     3   * Copyright 2021 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 ringhash
    20  
    21  import (
    22  	"encoding/json"
    23  	"fmt"
    24  
    25  	"github.com/hxx258456/ccgo/grpc/serviceconfig"
    26  )
    27  
    28  // LBConfig is the balancer config for ring_hash balancer.
    29  type LBConfig struct {
    30  	serviceconfig.LoadBalancingConfig `json:"-"`
    31  
    32  	MinRingSize uint64 `json:"minRingSize,omitempty"`
    33  	MaxRingSize uint64 `json:"maxRingSize,omitempty"`
    34  }
    35  
    36  const (
    37  	defaultMinSize = 1024
    38  	defaultMaxSize = 8 * 1024 * 1024 // 8M
    39  )
    40  
    41  func parseConfig(c json.RawMessage) (*LBConfig, error) {
    42  	var cfg LBConfig
    43  	if err := json.Unmarshal(c, &cfg); err != nil {
    44  		return nil, err
    45  	}
    46  	if cfg.MinRingSize == 0 {
    47  		cfg.MinRingSize = defaultMinSize
    48  	}
    49  	if cfg.MaxRingSize == 0 {
    50  		cfg.MaxRingSize = defaultMaxSize
    51  	}
    52  	if cfg.MinRingSize > cfg.MaxRingSize {
    53  		return nil, fmt.Errorf("min %v is greater than max %v", cfg.MinRingSize, cfg.MaxRingSize)
    54  	}
    55  	return &cfg, nil
    56  }