conf.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package conf
  2. import (
  3. "errors"
  4. "flag"
  5. "go-common/library/cache/redis"
  6. "go-common/library/conf"
  7. "go-common/library/database/hbase.v2"
  8. "go-common/library/database/sql"
  9. "go-common/library/log"
  10. bm "go-common/library/net/http/blademaster"
  11. xtime "go-common/library/time"
  12. "github.com/BurntSushi/toml"
  13. )
  14. // Conf global variable.
  15. var (
  16. Conf = &Config{}
  17. client *conf.Client
  18. confPath string
  19. )
  20. // Config struct of conf.
  21. type Config struct {
  22. // base
  23. // log
  24. Log *log.Config
  25. // mysql
  26. Mysql *sql.Config
  27. // http
  28. BM *bm.ServerConfig
  29. // hbase
  30. Hbase *HBaseConfig
  31. // redis
  32. Redis *Redis
  33. // extra property
  34. Property *Property
  35. }
  36. // HBaseConfig is.
  37. type HBaseConfig struct {
  38. *hbase.Config
  39. ReadTimeout xtime.Duration
  40. WriteTimeout xtime.Duration
  41. }
  42. // Redis redis.
  43. type Redis struct {
  44. *redis.Config
  45. Expire xtime.Duration
  46. }
  47. // Property figure conf
  48. type Property struct {
  49. PendingMidStart int64
  50. PendingMidShard int64
  51. PendingMidRetry int64
  52. CalcWeekOffset int64
  53. CycleCron string
  54. ConcurrencySize int64
  55. FixRecord bool
  56. CycleAll bool
  57. CycleAllCron string
  58. Calc *Calc
  59. }
  60. // Calc figure calc config
  61. type Calc struct {
  62. InitLawfulScore int64
  63. InitWideScore int64
  64. InitFriendlyScore int64
  65. InitBountyScore int64
  66. InitCreativityScore int64
  67. K1 float64
  68. K2 float64
  69. K3 float64
  70. K4 float64
  71. K5 float64
  72. LawfulNegMax float64
  73. LawfulPosMax float64
  74. LawfulPosK float64
  75. LawfulNegK1 float64
  76. LawfulNegK2 float64
  77. LawfulPosL float64
  78. LawfulNegL float64
  79. LawfulPosC1 float64
  80. LawfulPosC2 float64
  81. LawfulPosC3 float64
  82. LawfulNegC1 float64
  83. LawfulNegC2 float64
  84. LawfulNegC3 float64
  85. LawfulPosQ1 float64
  86. LawfulPosQ2 float64
  87. LawfulPosQ3 float64
  88. LawfulNegQ1 float64
  89. LawfulNegQ2 float64
  90. LawfulNegQ3 float64
  91. WidePosMax float64
  92. WidePosK float64
  93. WideC1 float64
  94. WideQ1 float64
  95. WideC2 float64
  96. WideQ2 float64
  97. FriendlyPosMax float64
  98. FriendlyNegMax float64
  99. FriendlyPosK float64
  100. FriendlyNegK float64
  101. FriendlyPosL float64
  102. FriendlyNegL float64
  103. FriendlyPosQ1 float64
  104. FriendlyPosC1 float64
  105. FriendlyPosQ2 float64
  106. FriendlyPosC2 float64
  107. FriendlyPosQ3 float64
  108. FriendlyPosC3 float64
  109. FriendlyNegQ1 float64
  110. FriendlyNegC1 float64
  111. FriendlyNegQ2 float64
  112. FriendlyNegC2 float64
  113. FriendlyNegQ3 float64
  114. FriendlyNegC3 float64
  115. FriendlyNegQ4 float64
  116. FriendlyNegC4 float64
  117. BountyMax float64
  118. BountyPosL float64
  119. BountyK float64
  120. BountyQ1 float64
  121. BountyC1 float64
  122. BountyQ2 float64
  123. BountyC2 float64
  124. BountyQ3 float64
  125. BountyC3 float64
  126. CreativityPosMax float64
  127. CreativityPosK float64
  128. CreativityPosL1 float64
  129. CreativityQ1 float64
  130. CreativityC1 float64
  131. }
  132. func init() {
  133. flag.StringVar(&confPath, "conf", "", "default config path")
  134. }
  135. // Init create config instance.
  136. func Init() (err error) {
  137. if confPath != "" {
  138. return local()
  139. }
  140. return remote()
  141. }
  142. func local() (err error) {
  143. _, err = toml.DecodeFile(confPath, &Conf)
  144. return
  145. }
  146. func remote() (err error) {
  147. if client, err = conf.New(); err != nil {
  148. return
  149. }
  150. if err = load(); err != nil {
  151. return
  152. }
  153. go func() {
  154. for range client.Event() {
  155. log.Info("config reload")
  156. if load() != nil {
  157. log.Error("config reload error (%v)", err)
  158. }
  159. }
  160. }()
  161. return
  162. }
  163. func load() (err error) {
  164. var (
  165. s string
  166. ok bool
  167. tmpConf *Config
  168. )
  169. if s, ok = client.Toml2(); !ok {
  170. return errors.New("load config center error")
  171. }
  172. if _, err = toml.Decode(s, &tmpConf); err != nil {
  173. return errors.New("could not decode config")
  174. }
  175. *Conf = *tmpConf
  176. return
  177. }