| khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 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" |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 24 | "strconv" |
| khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 25 | "strings" |
| 26 | ) |
| 27 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 28 | var ( |
| 29 | // EnableTXTServiceConfig is set if the DNS resolver should perform TXT |
| 30 | // lookups for service config ("GRPC_ENABLE_TXT_SERVICE_CONFIG" is not |
| 31 | // "false"). |
| 32 | EnableTXTServiceConfig = boolFromEnv("GRPC_ENABLE_TXT_SERVICE_CONFIG", true) |
| 33 | |
| 34 | // TXTErrIgnore is set if TXT errors should be ignored |
| 35 | // ("GRPC_GO_IGNORE_TXT_ERRORS" is not "false"). |
| 36 | TXTErrIgnore = boolFromEnv("GRPC_GO_IGNORE_TXT_ERRORS", true) |
| 37 | |
| 38 | // RingHashCap indicates the maximum ring size which defaults to 4096 |
| 39 | // entries but may be overridden by setting the environment variable |
| 40 | // "GRPC_RING_HASH_CAP". This does not override the default bounds |
| 41 | // checking which NACKs configs specifying ring sizes > 8*1024*1024 (~8M). |
| 42 | RingHashCap = uint64FromEnv("GRPC_RING_HASH_CAP", 4096, 1, 8*1024*1024) |
| 43 | |
| 44 | // ALTSMaxConcurrentHandshakes is the maximum number of concurrent ALTS |
| 45 | // handshakes that can be performed. |
| 46 | ALTSMaxConcurrentHandshakes = uint64FromEnv("GRPC_ALTS_MAX_CONCURRENT_HANDSHAKES", 100, 1, 100) |
| 47 | |
| 48 | // EnforceALPNEnabled is set if TLS connections to servers with ALPN disabled |
| 49 | // should be rejected. The HTTP/2 protocol requires ALPN to be enabled, this |
| 50 | // option is present for backward compatibility. This option may be overridden |
| 51 | // by setting the environment variable "GRPC_ENFORCE_ALPN_ENABLED" to "true" |
| 52 | // or "false". |
| 53 | EnforceALPNEnabled = boolFromEnv("GRPC_ENFORCE_ALPN_ENABLED", true) |
| 54 | |
| 55 | // NewPickFirstEnabled is set if the new pickfirst leaf policy is to be used |
| 56 | // instead of the exiting pickfirst implementation. This can be disabled by |
| 57 | // setting the environment variable "GRPC_EXPERIMENTAL_ENABLE_NEW_PICK_FIRST" |
| 58 | // to "false". |
| 59 | NewPickFirstEnabled = boolFromEnv("GRPC_EXPERIMENTAL_ENABLE_NEW_PICK_FIRST", true) |
| 60 | |
| 61 | // XDSEndpointHashKeyBackwardCompat controls the parsing of the endpoint hash |
| 62 | // key from EDS LbEndpoint metadata. Endpoint hash keys can be disabled by |
| 63 | // setting "GRPC_XDS_ENDPOINT_HASH_KEY_BACKWARD_COMPAT" to "true". When the |
| 64 | // implementation of A76 is stable, we will flip the default value to false |
| 65 | // in a subsequent release. A final release will remove this environment |
| 66 | // variable, enabling the new behavior unconditionally. |
| 67 | XDSEndpointHashKeyBackwardCompat = boolFromEnv("GRPC_XDS_ENDPOINT_HASH_KEY_BACKWARD_COMPAT", true) |
| 68 | |
| 69 | // RingHashSetRequestHashKey is set if the ring hash balancer can get the |
| 70 | // request hash header by setting the "requestHashHeader" field, according |
| 71 | // to gRFC A76. It can be enabled by setting the environment variable |
| 72 | // "GRPC_EXPERIMENTAL_RING_HASH_SET_REQUEST_HASH_KEY" to "true". |
| 73 | RingHashSetRequestHashKey = boolFromEnv("GRPC_EXPERIMENTAL_RING_HASH_SET_REQUEST_HASH_KEY", false) |
| 74 | |
| 75 | // ALTSHandshakerKeepaliveParams is set if we should add the |
| 76 | // KeepaliveParams when dial the ALTS handshaker service. |
| 77 | ALTSHandshakerKeepaliveParams = boolFromEnv("GRPC_EXPERIMENTAL_ALTS_HANDSHAKER_KEEPALIVE_PARAMS", false) |
| khenaidoo | ac63710 | 2019-01-14 15:44:34 -0500 | [diff] [blame] | 78 | ) |
| 79 | |
| Abhay Kumar | a2ae599 | 2025-11-10 14:02:24 +0000 | [diff] [blame^] | 80 | func boolFromEnv(envVar string, def bool) bool { |
| 81 | if def { |
| 82 | // The default is true; return true unless the variable is "false". |
| 83 | return !strings.EqualFold(os.Getenv(envVar), "false") |
| 84 | } |
| 85 | // The default is false; return false unless the variable is "true". |
| 86 | return strings.EqualFold(os.Getenv(envVar), "true") |
| 87 | } |
| 88 | |
| 89 | func uint64FromEnv(envVar string, def, min, max uint64) uint64 { |
| 90 | v, err := strconv.ParseUint(os.Getenv(envVar), 10, 64) |
| 91 | if err != nil { |
| 92 | return def |
| 93 | } |
| 94 | if v < min { |
| 95 | return min |
| 96 | } |
| 97 | if v > max { |
| 98 | return max |
| 99 | } |
| 100 | return v |
| 101 | } |