khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [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" |
Akash Kankanala | 761955c | 2024-02-21 19:32:20 +0530 | [diff] [blame^] | 24 | "strconv" |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 25 | "strings" |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 26 | ) |
| 27 | |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 28 | var ( |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 29 | // TXTErrIgnore is set if TXT errors should be ignored ("GRPC_GO_IGNORE_TXT_ERRORS" is not "false"). |
Akash Kankanala | 761955c | 2024-02-21 19:32:20 +0530 | [diff] [blame^] | 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 | // PickFirstLBConfig is set if we should support configuration of the |
| 40 | // pick_first LB policy, which can be enabled by setting the environment |
| 41 | // variable "GRPC_EXPERIMENTAL_PICKFIRST_LB_CONFIG" to "true". |
| 42 | PickFirstLBConfig = boolFromEnv("GRPC_EXPERIMENTAL_PICKFIRST_LB_CONFIG", false) |
khenaidoo | 5fc5cea | 2021-08-11 17:39:16 -0400 | [diff] [blame] | 43 | ) |
Akash Kankanala | 761955c | 2024-02-21 19:32:20 +0530 | [diff] [blame^] | 44 | |
| 45 | func boolFromEnv(envVar string, def bool) bool { |
| 46 | if def { |
| 47 | // The default is true; return true unless the variable is "false". |
| 48 | return !strings.EqualFold(os.Getenv(envVar), "false") |
| 49 | } |
| 50 | // The default is false; return false unless the variable is "true". |
| 51 | return strings.EqualFold(os.Getenv(envVar), "true") |
| 52 | } |
| 53 | |
| 54 | func uint64FromEnv(envVar string, def, min, max uint64) uint64 { |
| 55 | v, err := strconv.ParseUint(os.Getenv(envVar), 10, 64) |
| 56 | if err != nil { |
| 57 | return def |
| 58 | } |
| 59 | if v < min { |
| 60 | return min |
| 61 | } |
| 62 | if v > max { |
| 63 | return max |
| 64 | } |
| 65 | return v |
| 66 | } |