config.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "github.com/BurntSushi/toml"
  6. "go-common/library/conf"
  7. "go-common/library/log"
  8. xtime "go-common/library/time"
  9. )
  10. func init() {
  11. flag.StringVar(&confPath, "conf", "", "config file")
  12. }
  13. var (
  14. confPath string
  15. // Conf conf
  16. Conf = &Config{}
  17. client *conf.Client
  18. )
  19. // Config config.
  20. type Config struct {
  21. Log *log.Config `toml:"log"`
  22. HBase *HBaseConfig `toml:"hbase"`
  23. InfluxDB *InfluxDBConfig `toml:"influx_db"`
  24. Collect *Collect `toml:"collect"`
  25. BatchWriter *BatchWriter `toml:"batch_writer"`
  26. Dapper *DapperConfig `toml:"dapper"`
  27. KafkaCollect *KafkaCollect `toml:"kafka_collect"`
  28. }
  29. // DapperConfig .
  30. type DapperConfig struct {
  31. RetentionDay int `toml:"retention_day"`
  32. APIListen string `toml:"api_listen"`
  33. }
  34. // HBaseConfig hbase config
  35. type HBaseConfig struct {
  36. Namespace string `toml:"namespace"`
  37. Addrs string `toml:"addrs"`
  38. RPCQueueSize int `toml:"rpc_queue_size"`
  39. FlushInterval xtime.Duration `toml:"flush_interval"`
  40. EffectiveUser string `toml:"effective_user"`
  41. RegionLookupTimeout xtime.Duration `toml:"region_lookup_timeout"`
  42. RegionReadTimeout xtime.Duration `toml:"region_read_timeout"`
  43. }
  44. // InfluxDBConfig InfluxDBConfig
  45. type InfluxDBConfig struct {
  46. Addr string `toml:"addr"`
  47. Username string `toml:"username"`
  48. Password string `toml:"password"`
  49. Database string `toml:"database"`
  50. }
  51. // Collect config.
  52. type Collect struct {
  53. Network string
  54. Addr string
  55. }
  56. // KafkaCollect .
  57. type KafkaCollect struct {
  58. Topic string `toml:"topic"`
  59. Addrs []string `toml:"addrs"`
  60. }
  61. // BatchWriter config
  62. type BatchWriter struct {
  63. SummaryWorkers int `toml:"summary_workers"`
  64. SummaryBulkSize int `toml:"summary_bulk_size"`
  65. SummaryChanSize int `toml:"summary_chan_size"`
  66. RawWorkers int `toml:"raw_workers"`
  67. RawBufSize int `toml:"raw_buf_size"`
  68. RawChanSize int `toml:"raw_chan_size"`
  69. FlushInterval xtime.Duration `toml:"flush_interval"`
  70. }
  71. // Init config
  72. func Init() (err error) {
  73. if confPath != "" {
  74. return local()
  75. }
  76. return remote()
  77. }
  78. func local() (err error) {
  79. _, err = toml.DecodeFile(confPath, &Conf)
  80. return
  81. }
  82. func remote() (err error) {
  83. if client, err = conf.New(); err != nil {
  84. return
  85. }
  86. if err = load(); err != nil {
  87. return
  88. }
  89. go func() {
  90. for range client.Event() {
  91. log.Info("config reload")
  92. if err := load(); err != nil {
  93. log.Error("config reload error (%v)", err)
  94. }
  95. }
  96. }()
  97. return
  98. }
  99. func load() (err error) {
  100. var (
  101. s string
  102. ok bool
  103. tmpConf *Config
  104. )
  105. if s, ok = client.Value2("dapper-service.toml"); !ok {
  106. return errors.New("load config center error")
  107. }
  108. if _, err = toml.Decode(s, &tmpConf); err != nil {
  109. return errors.New("could not decode config")
  110. }
  111. *Conf = *tmpConf
  112. return
  113. }