google.golang.org/grpc@v1.72.2/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  	// RingHashCap indicates the maximum ring size which defaults to 4096
    32  	// entries but may be overridden by setting the environment variable
    33  	// "GRPC_RING_HASH_CAP".  This does not override the default bounds
    34  	// checking which NACKs configs specifying ring sizes > 8*1024*1024 (~8M).
    35  	RingHashCap = uint64FromEnv("GRPC_RING_HASH_CAP", 4096, 1, 8*1024*1024)
    36  	// LeastRequestLB is set if we should support the least_request_experimental
    37  	// LB policy, which can be enabled by setting the environment variable
    38  	// "GRPC_EXPERIMENTAL_ENABLE_LEAST_REQUEST" to "true".
    39  	LeastRequestLB = boolFromEnv("GRPC_EXPERIMENTAL_ENABLE_LEAST_REQUEST", false)
    40  	// ALTSMaxConcurrentHandshakes is the maximum number of concurrent ALTS
    41  	// handshakes that can be performed.
    42  	ALTSMaxConcurrentHandshakes = uint64FromEnv("GRPC_ALTS_MAX_CONCURRENT_HANDSHAKES", 100, 1, 100)
    43  	// EnforceALPNEnabled is set if TLS connections to servers with ALPN disabled
    44  	// should be rejected. The HTTP/2 protocol requires ALPN to be enabled, this
    45  	// option is present for backward compatibility. This option may be overridden
    46  	// by setting the environment variable "GRPC_ENFORCE_ALPN_ENABLED" to "true"
    47  	// or "false".
    48  	EnforceALPNEnabled = boolFromEnv("GRPC_ENFORCE_ALPN_ENABLED", true)
    49  	// XDSFallbackSupport is the env variable that controls whether support for
    50  	// xDS fallback is turned on. If this is unset or is false, only the first
    51  	// xDS server in the list of server configs will be used.
    52  	XDSFallbackSupport = boolFromEnv("GRPC_EXPERIMENTAL_XDS_FALLBACK", true)
    53  	// NewPickFirstEnabled is set if the new pickfirst leaf policy is to be used
    54  	// instead of the exiting pickfirst implementation. This can be disabled by
    55  	// setting the environment variable "GRPC_EXPERIMENTAL_ENABLE_NEW_PICK_FIRST"
    56  	// to "false".
    57  	NewPickFirstEnabled = boolFromEnv("GRPC_EXPERIMENTAL_ENABLE_NEW_PICK_FIRST", true)
    58  
    59  	// XDSEndpointHashKeyBackwardCompat controls the parsing of the endpoint hash
    60  	// key from EDS LbEndpoint metadata. Endpoint hash keys can be disabled by
    61  	// setting "GRPC_XDS_ENDPOINT_HASH_KEY_BACKWARD_COMPAT" to "true". When the
    62  	// implementation of A76 is stable, we will flip the default value to false
    63  	// in a subsequent release. A final release will remove this environment
    64  	// variable, enabling the new behavior unconditionally.
    65  	XDSEndpointHashKeyBackwardCompat = boolFromEnv("GRPC_XDS_ENDPOINT_HASH_KEY_BACKWARD_COMPAT", true)
    66  
    67  	// RingHashSetRequestHashKey is set if the ring hash balancer can get the
    68  	// request hash header by setting the "requestHashHeader" field, according
    69  	// to gRFC A76. It can be enabled by setting the environment variable
    70  	// "GRPC_EXPERIMENTAL_RING_HASH_SET_REQUEST_HASH_KEY" to "true".
    71  	RingHashSetRequestHashKey = boolFromEnv("GRPC_EXPERIMENTAL_RING_HASH_SET_REQUEST_HASH_KEY", false)
    72  )
    73  
    74  func boolFromEnv(envVar string, def bool) bool {
    75  	if def {
    76  		// The default is true; return true unless the variable is "false".
    77  		return !strings.EqualFold(os.Getenv(envVar), "false")
    78  	}
    79  	// The default is false; return false unless the variable is "true".
    80  	return strings.EqualFold(os.Getenv(envVar), "true")
    81  }
    82  
    83  func uint64FromEnv(envVar string, def, min, max uint64) uint64 {
    84  	v, err := strconv.ParseUint(os.Getenv(envVar), 10, 64)
    85  	if err != nil {
    86  		return def
    87  	}
    88  	if v < min {
    89  		return min
    90  	}
    91  	if v > max {
    92  		return max
    93  	}
    94  	return v
    95  }