conf.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/memcache"
  6. "go-common/library/cache/redis"
  7. "go-common/library/conf"
  8. "go-common/library/database/sql"
  9. ecode "go-common/library/ecode/tip"
  10. "go-common/library/log"
  11. "go-common/library/log/infoc"
  12. bm "go-common/library/net/http/blademaster"
  13. "go-common/library/net/http/blademaster/middleware/verify"
  14. "go-common/library/net/rpc/warden"
  15. "go-common/library/net/trace"
  16. xtime "go-common/library/time"
  17. "github.com/BurntSushi/toml"
  18. )
  19. var (
  20. confPath string
  21. client *conf.Client
  22. // Conf config
  23. Conf = &Config{}
  24. )
  25. // Config .
  26. type Config struct {
  27. Log *log.Config
  28. BM *bm.ServerConfig
  29. Verify *verify.Config
  30. Tracer *trace.Config
  31. Redis *redis.Config
  32. BFRedis *redis.Config
  33. Memcache *memcache.Config
  34. MySQL *sql.Config
  35. Ecode *ecode.Config
  36. Infoc *infoc.Config
  37. // grpc server
  38. GRPCServer *warden.ServerConfig
  39. GRPCClient map[string]*GRPCConfig
  40. // workpool
  41. WorkerPool *PoolConfig
  42. }
  43. // PoolConfig .
  44. type PoolConfig struct {
  45. MaxWorkers uint64
  46. MaxIdleWorkers uint64
  47. MinIdleWorkers uint64
  48. KeepAlive xtime.Duration
  49. }
  50. // GRPCConfig .
  51. type GRPCConfig struct {
  52. WardenConf *warden.ClientConfig
  53. Addr string
  54. }
  55. func init() {
  56. flag.StringVar(&confPath, "conf", "", "default config path")
  57. }
  58. // Init init conf
  59. func Init() error {
  60. if confPath != "" {
  61. return local()
  62. }
  63. return remote()
  64. }
  65. func local() (err error) {
  66. _, err = toml.DecodeFile(confPath, &Conf)
  67. return
  68. }
  69. func remote() (err error) {
  70. if client, err = conf.New(); err != nil {
  71. return
  72. }
  73. if err = load(); err != nil {
  74. return
  75. }
  76. go func() {
  77. for range client.Event() {
  78. log.Info("config reload")
  79. if load() != nil {
  80. log.Error("config reload error (%v)", err)
  81. }
  82. }
  83. }()
  84. return
  85. }
  86. func load() (err error) {
  87. var (
  88. s string
  89. ok bool
  90. tmpConf *Config
  91. )
  92. if s, ok = client.Toml2(); !ok {
  93. return errors.New("load config center error")
  94. }
  95. if _, err = toml.Decode(s, &tmpConf); err != nil {
  96. return err
  97. }
  98. *Conf = *tmpConf
  99. return
  100. }