envconfig.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. // Package envconfig contains grpc settings configured by environment variables.
  19. package envconfig
  20. import (
  21. "os"
  22. "strconv"
  23. "strings"
  24. )
  25. var (
  26. // TXTErrIgnore is set if TXT errors should be ignored ("GRPC_GO_IGNORE_TXT_ERRORS" is not "false").
  27. TXTErrIgnore = boolFromEnv("GRPC_GO_IGNORE_TXT_ERRORS", true)
  28. // AdvertiseCompressors is set if registered compressor should be advertised
  29. // ("GRPC_GO_ADVERTISE_COMPRESSORS" is not "false").
  30. AdvertiseCompressors = boolFromEnv("GRPC_GO_ADVERTISE_COMPRESSORS", 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. // PickFirstLBConfig is set if we should support configuration of the
  37. // pick_first LB policy, which can be enabled by setting the environment
  38. // variable "GRPC_EXPERIMENTAL_PICKFIRST_LB_CONFIG" to "true".
  39. PickFirstLBConfig = boolFromEnv("GRPC_EXPERIMENTAL_PICKFIRST_LB_CONFIG", false)
  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. func uint64FromEnv(envVar string, def, min, max uint64) uint64 {
  50. v, err := strconv.ParseUint(os.Getenv(envVar), 10, 64)
  51. if err != nil {
  52. return def
  53. }
  54. if v < min {
  55. return min
  56. }
  57. if v > max {
  58. return max
  59. }
  60. return v
  61. }