conf.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. "go-common/library/log"
  10. "go-common/library/log/infoc"
  11. bm "go-common/library/net/http/blademaster"
  12. "go-common/library/net/http/blademaster/middleware/antispam"
  13. "go-common/library/net/http/blademaster/middleware/rate"
  14. v "go-common/library/net/http/blademaster/middleware/verify"
  15. "go-common/library/net/rpc"
  16. "go-common/library/net/trace"
  17. "go-common/library/queue/databus"
  18. "go-common/library/time"
  19. "github.com/BurntSushi/toml"
  20. "go-common/library/net/rpc/warden"
  21. )
  22. // Conf global variable.
  23. var (
  24. confPath string
  25. Conf = &Config{}
  26. client *conf.Client
  27. )
  28. // Config struct of conf.
  29. type Config struct {
  30. Host *Host
  31. // bm
  32. BM *bm.ServerConfig
  33. // log
  34. Log *log.Config
  35. // rpc server2
  36. RPCServer *rpc.ServerConfig
  37. // db
  38. Mysql *sql.Config
  39. // mc
  40. Memcache *Memcache
  41. // redis
  42. Redis *Redis
  43. // tracer
  44. Tracer *trace.Config
  45. // realtion
  46. Relation *Relation
  47. // rpc clients
  48. RPCClient2 *RPC
  49. // Infoc
  50. Infoc *infoc.Config
  51. // Antispam
  52. Antispam *antispam.Config
  53. // statCache
  54. StatCache *StatCache
  55. // httpClinet
  56. HTTPClient *bm.ClientConfig
  57. // Report
  58. Report *databus.Config
  59. // Verify
  60. Verify *v.Config
  61. // Rate
  62. AddFollowingRate *rate.Config
  63. // WardenServer
  64. WardenServer *warden.ServerConfig
  65. }
  66. // Host host.
  67. type Host struct {
  68. Passport string
  69. }
  70. // RPC clients config.
  71. type RPC struct {
  72. // member rpc client
  73. Member *rpc.ClientConfig
  74. }
  75. // Redis redis
  76. type Redis struct {
  77. *redis.Config
  78. Expire time.Duration
  79. }
  80. // Memcache memcache
  81. type Memcache struct {
  82. *memcache.Config
  83. Expire time.Duration
  84. FollowerExpire time.Duration
  85. }
  86. // Relation relation related config
  87. type Relation struct {
  88. MaxFollowingCached int
  89. MaxFollowerCached int
  90. MaxWhisperCached int
  91. // max following limit
  92. MaxFollowingLimit int
  93. // max black limit
  94. MaxBlackLimit int
  95. // monitor switch: true, user cannot be following.
  96. Monitor bool
  97. // prompt
  98. Period time.Duration // prompt count flush period
  99. Bcount int64 // business prompt count
  100. Ucount int64 // up prompt count
  101. // followers unread duration
  102. FollowersUnread time.Duration
  103. // achieve key
  104. AchieveKey string
  105. }
  106. // StatCache is
  107. type StatCache struct {
  108. Size int
  109. Expire time.Duration
  110. LeastFollower int
  111. }
  112. func init() {
  113. flag.StringVar(&confPath, "conf", "", "default config path")
  114. }
  115. // Init init conf.
  116. func Init() (err error) {
  117. if confPath != "" {
  118. return local()
  119. }
  120. return remote()
  121. }
  122. func local() (err error) {
  123. _, err = toml.DecodeFile(confPath, &Conf)
  124. return
  125. }
  126. func remote() (err error) {
  127. if client, err = conf.New(); err != nil {
  128. return
  129. }
  130. if err = load(); err != nil {
  131. return
  132. }
  133. go func() {
  134. for range client.Event() {
  135. log.Info("config reload")
  136. if load() != nil {
  137. log.Error("config reload error (%v)", err)
  138. }
  139. }
  140. }()
  141. return
  142. }
  143. func load() (err error) {
  144. var (
  145. s string
  146. ok bool
  147. tmpConf *Config
  148. )
  149. if s, ok = client.Toml2(); !ok {
  150. return errors.New("load config center error")
  151. }
  152. if _, err = toml.Decode(s, &tmpConf); err != nil {
  153. return errors.New("could not decode config")
  154. }
  155. *Conf = *tmpConf
  156. return
  157. }