google.golang.org/grpc@v1.62.1/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 "google.golang.org/grpc/internal/envconfig" 26 "google.golang.org/grpc/serviceconfig" 27 ) 28 29 // LBConfig is the balancer config for ring_hash balancer. 30 type LBConfig struct { 31 serviceconfig.LoadBalancingConfig `json:"-"` 32 33 MinRingSize uint64 `json:"minRingSize,omitempty"` 34 MaxRingSize uint64 `json:"maxRingSize,omitempty"` 35 } 36 37 const ( 38 defaultMinSize = 1024 39 defaultMaxSize = 4096 40 ringHashSizeUpperBound = 8 * 1024 * 1024 // 8M 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 if cfg.MinRingSize > ringHashSizeUpperBound { 49 return nil, fmt.Errorf("min_ring_size value of %d is greater than max supported value %d for this field", cfg.MinRingSize, ringHashSizeUpperBound) 50 } 51 if cfg.MaxRingSize > ringHashSizeUpperBound { 52 return nil, fmt.Errorf("max_ring_size value of %d is greater than max supported value %d for this field", cfg.MaxRingSize, ringHashSizeUpperBound) 53 } 54 if cfg.MinRingSize == 0 { 55 cfg.MinRingSize = defaultMinSize 56 } 57 if cfg.MaxRingSize == 0 { 58 cfg.MaxRingSize = defaultMaxSize 59 } 60 if cfg.MinRingSize > cfg.MaxRingSize { 61 return nil, fmt.Errorf("min %v is greater than max %v", cfg.MinRingSize, cfg.MaxRingSize) 62 } 63 if cfg.MinRingSize > envconfig.RingHashCap { 64 cfg.MinRingSize = envconfig.RingHashCap 65 } 66 if cfg.MaxRingSize > envconfig.RingHashCap { 67 cfg.MaxRingSize = envconfig.RingHashCap 68 } 69 return &cfg, nil 70 }