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