blob: 80fd5c7d2a4f42358686ce41f3f7a8523231dc14 [file] [log] [blame]
khenaidoo5fc5cea2021-08-11 17:39:16 -04001/*
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.
20package envconfig
21
22import (
23 "os"
Akash Kankanala761955c2024-02-21 19:32:20 +053024 "strconv"
khenaidoo5fc5cea2021-08-11 17:39:16 -040025 "strings"
khenaidoo5fc5cea2021-08-11 17:39:16 -040026)
27
khenaidoo5fc5cea2021-08-11 17:39:16 -040028var (
khenaidoo5fc5cea2021-08-11 17:39:16 -040029 // TXTErrIgnore is set if TXT errors should be ignored ("GRPC_GO_IGNORE_TXT_ERRORS" is not "false").
Akash Kankanala761955c2024-02-21 19:32:20 +053030 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)
khenaidoo5fc5cea2021-08-11 17:39:16 -040043)
Akash Kankanala761955c2024-02-21 19:32:20 +053044
45func 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
54func 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}