envconfig.go (2024B)
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 ) 40 41 func boolFromEnv(envVar string, def bool) bool { 42 if def { 43 // The default is true; return true unless the variable is "false". 44 return !strings.EqualFold(os.Getenv(envVar), "false") 45 } 46 // The default is false; return false unless the variable is "true". 47 return strings.EqualFold(os.Getenv(envVar), "true") 48 } 49 50 func uint64FromEnv(envVar string, def, min, max uint64) uint64 { 51 v, err := strconv.ParseUint(os.Getenv(envVar), 10, 64) 52 if err != nil { 53 return def 54 } 55 if v < min { 56 return min 57 } 58 if v > max { 59 return max 60 } 61 return v 62 }