google.golang.org/grpc@v1.72.2/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  	"strings"
    25  
    26  	"google.golang.org/grpc/internal/envconfig"
    27  	"google.golang.org/grpc/internal/metadata"
    28  	"google.golang.org/grpc/serviceconfig"
    29  )
    30  
    31  // LBConfig is the balancer config for ring_hash balancer.
    32  type LBConfig struct {
    33  	serviceconfig.LoadBalancingConfig `json:"-"`
    34  
    35  	MinRingSize       uint64 `json:"minRingSize,omitempty"`
    36  	MaxRingSize       uint64 `json:"maxRingSize,omitempty"`
    37  	RequestHashHeader string `json:"requestHashHeader,omitempty"`
    38  }
    39  
    40  const (
    41  	defaultMinSize         = 1024
    42  	defaultMaxSize         = 4096
    43  	ringHashSizeUpperBound = 8 * 1024 * 1024 // 8M
    44  )
    45  
    46  func parseConfig(c json.RawMessage) (*LBConfig, error) {
    47  	var cfg LBConfig
    48  	if err := json.Unmarshal(c, &cfg); err != nil {
    49  		return nil, err
    50  	}
    51  	if cfg.MinRingSize > ringHashSizeUpperBound {
    52  		return nil, fmt.Errorf("min_ring_size value of %d is greater than max supported value %d for this field", cfg.MinRingSize, ringHashSizeUpperBound)
    53  	}
    54  	if cfg.MaxRingSize > ringHashSizeUpperBound {
    55  		return nil, fmt.Errorf("max_ring_size value of %d is greater than max supported value %d for this field", cfg.MaxRingSize, ringHashSizeUpperBound)
    56  	}
    57  	if cfg.MinRingSize == 0 {
    58  		cfg.MinRingSize = defaultMinSize
    59  	}
    60  	if cfg.MaxRingSize == 0 {
    61  		cfg.MaxRingSize = defaultMaxSize
    62  	}
    63  	if cfg.MinRingSize > cfg.MaxRingSize {
    64  		return nil, fmt.Errorf("min %v is greater than max %v", cfg.MinRingSize, cfg.MaxRingSize)
    65  	}
    66  	if cfg.MinRingSize > envconfig.RingHashCap {
    67  		cfg.MinRingSize = envconfig.RingHashCap
    68  	}
    69  	if cfg.MaxRingSize > envconfig.RingHashCap {
    70  		cfg.MaxRingSize = envconfig.RingHashCap
    71  	}
    72  	if !envconfig.RingHashSetRequestHashKey {
    73  		cfg.RequestHashHeader = ""
    74  	}
    75  	if cfg.RequestHashHeader != "" {
    76  		cfg.RequestHashHeader = strings.ToLower(cfg.RequestHashHeader)
    77  		// See rules in https://github.com/grpc/proposal/blob/master/A76-ring-hash-improvements.md#explicitly-setting-the-request-hash-key
    78  		if err := metadata.ValidateKey(cfg.RequestHashHeader); err != nil {
    79  			return nil, fmt.Errorf("invalid requestHashHeader %q: %v", cfg.RequestHashHeader, err)
    80  		}
    81  		if strings.HasSuffix(cfg.RequestHashHeader, "-bin") {
    82  			return nil, fmt.Errorf("invalid requestHashHeader %q: key must not end with \"-bin\"", cfg.RequestHashHeader)
    83  		}
    84  	}
    85  	return &cfg, nil
    86  }