google.golang.org/grpc@v1.62.1/internal/envconfig/envconfig.go (about)

     1  /*
     2   *
     3   * Copyright 2018 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 envconfig contains grpc settings configured by environment variables.
    20  package envconfig
    21  
    22  import (
    23  	"os"
    24  	"strconv"
    25  	"strings"
    26  )
    27  
    28  var (
    29  	// TXTErrIgnore is set if TXT errors should be ignored ("GRPC_GO_IGNORE_TXT_ERRORS" is not "false").
    30  	TXTErrIgnore = boolFromEnv("GRPC_GO_IGNORE_TXT_ERRORS", true)
    31  	// AdvertiseCompressors is set if registered compressor should be advertised
    32  	// ("GRPC_GO_ADVERTISE_COMPRESSORS" is not "false").
    33  	AdvertiseCompressors = boolFromEnv("GRPC_GO_ADVERTISE_COMPRESSORS", true)
    34  	// RingHashCap indicates the maximum ring size which defaults to 4096
    35  	// entries but may be overridden by setting the environment variable
    36  	// "GRPC_RING_HASH_CAP".  This does not override the default bounds
    37  	// checking which NACKs configs specifying ring sizes > 8*1024*1024 (~8M).
    38  	RingHashCap = uint64FromEnv("GRPC_RING_HASH_CAP", 4096, 1, 8*1024*1024)
    39  	// LeastRequestLB is set if we should support the least_request_experimental
    40  	// LB policy, which can be enabled by setting the environment variable
    41  	// "GRPC_EXPERIMENTAL_ENABLE_LEAST_REQUEST" to "true".
    42  	LeastRequestLB = boolFromEnv("GRPC_EXPERIMENTAL_ENABLE_LEAST_REQUEST", false)
    43  	// ALTSMaxConcurrentHandshakes is the maximum number of concurrent ALTS
    44  	// handshakes that can be performed.
    45  	ALTSMaxConcurrentHandshakes = uint64FromEnv("GRPC_ALTS_MAX_CONCURRENT_HANDSHAKES", 100, 1, 100)
    46  )
    47  
    48  func boolFromEnv(envVar string, def bool) bool {
    49  	if def {
    50  		// The default is true; return true unless the variable is "false".
    51  		return !strings.EqualFold(os.Getenv(envVar), "false")
    52  	}
    53  	// The default is false; return false unless the variable is "true".
    54  	return strings.EqualFold(os.Getenv(envVar), "true")
    55  }
    56  
    57  func uint64FromEnv(envVar string, def, min, max uint64) uint64 {
    58  	v, err := strconv.ParseUint(os.Getenv(envVar), 10, 64)
    59  	if err != nil {
    60  		return def
    61  	}
    62  	if v < min {
    63  		return min
    64  	}
    65  	if v > max {
    66  		return max
    67  	}
    68  	return v
    69  }